File SMJavaCollections.mys
Collection and Map inspired from Java programming language.
As a Java developer since more than 20 years, I'm used to work with
Collection and Map which offer powerful methods.
Here there are in Lua!
SMJavaCollections.mys is automatically included when you Include "SMUtils"
.
Differences between table and Collection
Better than long explanations, here are code/algorithm that demonstrate the differences.
Action | table | Collection | Notes |
---|---|---|---|
Creation | t = {} |
c = Collection:new(elementType, sortFunction) |
where elementType is "number" , "string" or "table" |
Add | tinsert(t, data) |
c:add(data) |
In the same table, you can insert numbers, strings and others types. Collection avoid this: more restrictive, less bugs. |
Add a set of data | for i=1,getn(datas) do tinsert(t, datas[i]) end |
c:addAll(anotherCollection) |
|
Read | print(t[2]) |
print(c:get(2)) |
With Collection, the data is guaranteed to have the type you want. |
Remove data at index 2 | tremove(t, 2) |
c:removeAt(2) |
|
Remove first "banana" | for i=1,getn(t) do if t[i]=="banana" then tremove(t, i) break end end |
c:remove("banana") |
|
Remove all "bananas" | Loop backward, move efficientfor i=getn(t),1,-1 do if t[i]=="banana" then tremove(t, i) end end |
c:removeAll("bananas") |
|
Get size | getn(t) |
c:size() |
|
Replace at index 2 | t[2] = "apple" |
c:replaceAt(2, "apple") |
|
Sort | sort(t, sortFunction) |
Nothing to do | Datas are sorted at insertion, if Collection is created with a sorting function |
Does it contain "apricot"? |
|
c:contains("apricot") |
|
Don't add "banana" if already in table | Loop. if no "banana" found then insert.local found = false for i=1,getn(t) do if t[i]=="banana" then found=true; break; end end if found==false then tinsert(t, "banana") end |
Nothing to do | At creation time, Collection may be a "set", i.e. a Collection of unique datas |
Get index of "apple" |
|
c:indexOf("apple") |
|
Get the first / last data |
|
c:first() or c:last() |
|
Get greatest/least data |
|
c:last() / c:first() |
for sorted Collection only |
Get all data before/after (or including) "banana" |
|
c:head("banana", inclusive) or c:tail("banana", inclusive) where inclusive is a boolean. |
|
Browse | for i=1,getn(t) do print(t[i]) end Backward browsing: for i=getn(t),1,-1 do...
|
c:iterator() while c:hasNext() do print(c:next()) end Backward browsing: c:reverseIterator() |
As you can notice, very simple operation are easier to write using table, but complex ones are really more friendly using Collection.
Collection should save you time, make your script more maintainables, and reduce errors because of type-protection of inserted datas.
Note on performance: Collection is backed on native Lua table. It's fast, but just a little bit less than working with tables.
If you are really concerned by CPU and memory optimization and perform a lot of simple operation on many tables,
you may prefer table to Collection.
If you are concerned by easy-to-read code and ready-to-use functions, Collection is for you.
For example, Collection is usless for building a large table of digital waveform (44100 numbers per second of sound).
Classes
Class | Summary |
---|---|
Collection | Collection empowers table: unicity, sort and type-protection of datas (elements). |
Map | Map is a Collection of keys with associated value for each key. |
Summary
Constant | Type | Summary |
---|---|---|
SORT_ASCENDING(a, b) | function | Natural ascending ordering function |
SORT_DESCENDING(a, b) | function | Natural descending ordering function |
Constants
function SORT_ASCENDING(a, b)
Natural ascending ordering function
Parameter | Type | Default | Description |
---|---|---|---|
a | a | ||
b | b |
- Usage
my_collection = Collection:new("string", true, SORT_ASCENDING)
function SORT_DESCENDING(a, b)
Natural descending ordering function
Parameter | Type | Default | Description |
---|---|---|---|
a | a | ||
b | b |
- Usage
my_collection = Collection:new("string", true, SORT_DESCENDING)