Class Map
Map is a Collection of keys with associated value for each key.
Each pair key->value is called entry.
- Keys and values are type-protected
- Keys are number or string
- Keys are unique
- Keys can be sorted or stay in insertion order
- Values are number, string or table.
As table are the root of any object/class, you can store almost everything as a values, e.g. Symbols, or another Map.
A special"mixed"
argument removes the type-protection for values.
If you've already read Collection documentation, you should have understood that keys are in a Set (a Collection of unique elements).
If not, please read it before ;)
Differences between table and Map
Map is like Lua table where indices are replaced by string like in
births["Bach"] = 1685
. In this example, key is the string "Bach"
and the value is the number 1685
.
Map is backed by two Collections called keySet and values.
Map accepts decimal numbers keys. Lua doesn't handle them as number:
t={}; t[1] = "unity"; t[3.14] = "pi"; t[10] = "ten"; sort(t);
produces error
error: attempt to compare nil with string.
Here are some code examples
Action | Lua table | Map | Notes |
---|---|---|---|
Creation | t = {} |
m = Map:new(keyType, valueType, sortFunction) |
where keyType is "number" or "string" ,
and valueType is "number", "string", "table" or "mixed"
|
Add entry | t["Key"] = value |
m:put(key, value) |
key must be of keyType, and value of valueType |
Add multiple entries
local source = {key1=value1, key2=value2} local dest = ... |
for k,v in source do dest[k] = v end |
dest:putAll(source) |
|
Read | print(t[key]) |
print(m:get(key)) |
|
Contain a key? | if t[key] then ... end |
if m:containsKey(key) then ... end |
|
Contain a value? | local found = false for k,v in d do if v=="wanted" then found=true; break; end end if found then print("Found") else print("Not found"); end |
if m:containsValue(wanted) then print("Found") else print("Not found"); end |
|
Get size | getn(t) is bogus:
local t = {Key1=1, key2=2} print(getn(t)) --> 0 |
m:size() |
|
Browse | local t = {key1=1, key2=2} for k,v in datas do print(k.."="..v) end Backward browsing impossible |
m:iterator() while m:hasNext() do plocal k,v = m:next() print(k.."="..v) end Backward browsing: m:reverseIterator() |
Summary
Return type | Function and summary |
---|---|
Map | new(string keyType, string valueType, function sortFunction) Create a new Map. |
keyType, valueType | ceilingEntry(keyType key) Returns key and value associated with the least key greater than or equal to the given key, or nil,nil if there is no such key. |
keyType | ceilingKey(keyType key) Returns the least key greater than or equal to the given key, or nil if there is no such key. |
clear() Empty the map, remove all keys and values | |
int | concat(keyType key, string value, string separator) If this Map contains the given key, concatenate the given value to existing value, else create a new entry keyand value. |
boolean | containsKey(keyType key) Does Map already contains the given key? |
boolean | containsValue(valueType value) Does Map already contains the given value? |
debug() Debug a structure of this Map in console, only if LOG_LEVEL = LOG_LEVEL_DEBUG . | |
keyType, valueType | firstEntry() Returns the first (least if Map is sorted) entry (key and value) of this Map, or nil,nil if the Map is empty. |
keyType | firstKey() Returns the first (least if Map is sorted) key of this Map, or nil if the Map is empty. |
keyType, valueType | floorEntry(keyType key) Returns key and mapped value associated with the greatest key less or equal than the given key, or nil,nil if there is no such key. |
keyType | floorKey(keyType key) Returns the greatest key less or equal than the given key, or nil if there is no such key. |
valueType | get(key) Returns the value associated to the given key, or nil if this map doesn't contains it. |
valueType | getOrDefault(keyType key, valueType defaultValue) Returns the value associated to the given key, or defaultValue if this map doesn't contains it. |
hasNext(string iteratorName) While iterating, is there a next entry? | |
Map | headMap(keyType toKey, boolean inclusive) Returns a view of the portion of this map whose keys are less than (or equal to, if inclusive = true) toKey. |
keyType, valueType | higherEntry(keyType key) Returns entry (key,value) of the least key greater than the given key, or nil,nil if there is no such key. |
keyType | higherKey(keyType key) Returns the least key greater than the given key, or nil if there is no such key. |
boolean | isEmpty() Is this Map empty? |
iterator(string iteratorName) Starts or restarts an iterator on the Map. | |
Collection | keySet() Returns a Set view of the keys contained in this Map. |
keyType, valueType | lastEntry() Returns the last (greatest if Map is sorted) entry (key,value) of this Map, or nil,nil if this Map is empty. |
keyType, keyType, valueType | lastKey() Returns the last (greatest if Map is sorted) key of this Map, or nil if Map is empty. |
keyType, valueType | lowerEntry(keyType key) Returns key,value of the greatest key less than the given key, or nil,nil if there is no such key. |
keyType | lowerKey(keyType key) Returns the greatest key less than the given key, or nil if there is no such key. |
int | merge(keyType key, valueType value) If this Map doesn't contains the given key, insert the given key and value. |
next(string iteratorName) While iterating, returns next entry if :hasNext() , nil,nil otherwise. | |
print() Prints a debug structure of the Map in console, if you want it prints only if LOG_LEVEL = LOG_LEVEL_DEBUG , use :debug() instead. | |
int | put(keyType key, valueType value) Insert a new entry (the given key,value) into this Map. |
int | putAll(anotherMap) Put all entries (key,value pairs) of anotherMap into this Map. |
int | remove(keyType key, valueType value) Removes the entry for the given key. |
int | removeAll(elements) From a given table, Collection or Map, remove found elements in this Map. |
int | replace(keyType key, valueType oldValue, valueType newValue) Replaces the entry if this Map contains key, never inserts new entry. |
reverseIterator(string iteratorName) Starts or restarts a reverse iterator on the Map. | |
Map | searchEntries(string regexp, boolean caseInsensitive) If keys or values are strings, search by a regexp and return a Map of results. |
Collection | searchKeys(string regexp, boolean caseInsensitive) If the keys are strings, search by a regexp and return a Collection of results. |
Collection | searchValues(string regexp, boolean caseInsensitive) If the values are strings, search by a regexp and return a Collection of results. |
int | size() Returns the number of entries in this Map. |
Map | subMap(keyType fromKey, boolean fromInclusive, keyType toKey, boolean toInclusive) Returns a view of the portion of this Map whose keys range from fromKey to toKey. |
Map | tailMap(keyType fromKey, boolean inclusive) Returns a view of the portion of this Map whose keys are greater than (or equal to, if inclusive==true) fromKey. |
Collection | values() Returns a Collection view of the values contained in this Map, in the same order than the keys. |
Methods
Map:new(string keyType, string valueType, function sortFunction)
Create a new Map.
Parameter | Type | Default | Description |
---|---|---|---|
keyType | string | "string" | "number" or "string" |
valueType | string | "string" | "number" , "string" , "table" a Lua class or "mixed" . |
sortFunction | function | nil | keys sorting function can be provided, else keys stay in insertion order |
- Return
- Map: A new empty Map
- Error
- if keyType or valueType have wrong values.
- Example
local m = Map:new("string", "number") local sortedMap = Map:new("number", "string", SORT_ASCENDING) local chords = Map:new("string", "Chord")
Map:put(keyType key, valueType value)
Insert a new entry (the given key,value) into this Map.
If key already exists, its old value is replaced by the given value.
Parameter | Type | Default | Description |
---|---|---|---|
key | keyType | Key to insert or update, number or string | |
value | valueType | New value |
- Return
- int: Always 1
- Errors
- if
type(key)
doesn't match constructor keyType parameter - if
type(value)
doesn't match constructor valueType parameter - Example
local m = Map:new("string", "number") m:put("Bach", 1685); m:put("Mozart", 1756)
Map:putAll(anotherMap)
Put all entries (key,value pairs) of anotherMap into this Map.
If some key already exist, their associated value are replaced.
Parameter | Type | Default | Description |
---|---|---|---|
anotherMap | A table[key]=value , or a Map |
- Return
- int: Number of inserted/updated pairs, = anotherMap:size().
- Errors
- if anotherMap is not a table nor a Map.
- if anotherMap entries (key,value) are not compatibles with constructor keyType and valueType parameters.
- Example
local m = Map:new("string", "number") local t = {}; t.Bach = 1685; t["Mozart"] = 1756 m:putAll(t)
Map:clear()
Empty the map, remove all keys and values
Map:containsKey(keyType key)
Does Map already contains the given key?
Parameter | Type | Default | Description |
---|---|---|---|
key | keyType | number or string |
- Return
- boolean:
true
, ornil
(for false) - Error
- if
type(key)
doesn't match constructor keyType parameter - Example
local m = Map:new("string", "number") m:putAll({Bach=1685, Mozart=1756}) print(m:containsKey("Bach")) --> true print(m:containsKey("HAydn")) --> false
Map:containsValue(valueType value)
Does Map already contains the given value?
Parameter | Type | Default | Description |
---|---|---|---|
value | valueType | number, string or table |
- Return
- boolean:
true
, ornil
(for false) - Errors
- if
type(value)
doesn't match constructor valueType parameter - if values of this Map are not comparables
- Example
local m = Map:new("string", "number") m:putAll({Bach=1685, Mozart=1756}) print(m:containsValue(1756)) --> true print(m:containsValue(1789)) --> false
Map:get(key)
Returns the value associated to the given key, or nil
if this map doesn't contains it.
Parameter | Type | Default | Description |
---|---|---|---|
key | number or string |
- Return
- valueType: A value or
nil
if key is not found - Error
- if
type(key)
doesn't match constructor keyType parameter - Example
local m = Map:new("string", "number") m:putAll({Bach=1685, Mozart=1756}) print(m:get("Bach")) --> 1685 print(m:get("Haydn")) --> nil
Map:getOrDefault(keyType key, valueType defaultValue)
Returns the value associated to the given key, or defaultValue if this map doesn't contains it.
Parameter | Type | Default | Description |
---|---|---|---|
key | keyType | number or string | |
defaultValue | valueType | number, string or table |
- Return
- valueType: A value or defaultValue if key is not found
- Errors
- if
type(key)
doesn't match constructor keyType parameter - if
type(defaultValue)
doesn't match constructor valueType parameter - Example
local m = Map:new("string", "number") m:putAll({Bach=1685, Mozart=1756}) print(m:getOrDefault("Bach", 1234)) --> 1685 print(m:getOrDefault("Haydn", 1234)) --> 1234
Map:keySet()
Returns a Set view of the keys contained in this Map.
A Set is a Collection with unique elements, sorted or not.
- Return
- Collection: A Collection containing all keys
Map:concat(keyType key, string value, string separator)
If this Map contains the given key, concatenate the given value to existing value, else create a new entry keyand value.
Apply only for string value type.
Parameter | Type | Default | Description |
---|---|---|---|
key | keyType | The key to insert if not found, or find the existing value to be concatenated. | |
value | string | The string to concatenate to existing value. | |
separator | string | ", " | Separator between existing value and the new value to concatenate. |
- Return
- int: 0 if concatenation was done, 1if a new entry was inserted.
- Errors
- if
type(key)
doesn't match constructor keyType parameter - if
type(value)
doesn't match constructor valueType parameter - Example
local m = Map:new("string", "number") m:put("Baroque", "Bach") print(m:get("Baroque")) --> Bach -- add Vivaldi to Baroque composers m:concat("Baroque", "Vivaldi") --> return 0 print(m:get("Baroque")) --> Bach, Vivaldi -- add Mozart to Classical composers m:concat("Classical", "Mozart") --> return 1 print(m:get("Classical")) --> Mozart
Map:merge(keyType key, valueType value)
If this Map doesn't contains the given key, insert the given key and value.
Else, no change is done, the given value will not replace the old associated to key.
Parameter | Type | Default | Description |
---|---|---|---|
key | keyType | The key to insert if not found, number or string | |
value | valueType | The value |
- Return
- int: 1 if key,value is inserted, 0 if key already exists in this Map.
- Errors
- if
type(key)
doesn't match constructor keyType parameter - if
type(value)
doesn't match constructor valueType parameter - Example
local m = Map:new("string", "number") m:putAll({Bach=1685, Mozart=1756}) print(m:get("Vivaldi")) --> nil -- add Vivaldi if not already in map m:merge("Vivaldi", 1678) --> return 1 print(m:get("Vivaldi")) --> 1678 -- merge with a bogus date m:merge("Bach", 1785) --> return 0 print(m:get("Bach")) --> 1685, not replaced
Map:remove(keyType key, valueType value)
Removes the entry for the given key.
If the given value is not nil
, removes the entry only if its value equals the given value.
Parameter | Type | Default | Description |
---|---|---|---|
key | keyType | number or string | |
value | valueType | nil | A value to remove only if equals, or nil to remove without testing equality check |
- Return
- int: 0 if not removed, 1 if removed.
- Errors
- if
type(key)
doesn't match constructor keyType parameter - if
type(value)
doesn't match constructor valueType parameter - if value~=nil and values of this Map are not comparables
- Example
local m = Map:new("string", "number") m:putAll({Bach=1685, Mozart=1756}) m:remove("Vivaldi") --> return 0 m:remove("Bach", 1234) --> return 0, not removed because Bach value is 1685 m:remove("Bach", 1685) --> return 1 m:remove("Mozart") --> return 1
Map:removeAll(elements)
From a given table, Collection or Map, remove found elements in this Map.
if elements is a Map, then key and value type must match this Map, and removals follow the Map:remove(key, value)
rules.
If elements is a table or Collection, they must contain keys to remove, there is no value equality check as Map:remove(key)
.
Parameter | Type | Default | Description |
---|---|---|---|
elements | table, Collection or Map |
- Return
- int: Number of removals, between 0 and Map:size()
Map:replace(keyType key, valueType oldValue, valueType newValue)
Replaces the entry if this Map contains key, never inserts new entry.
If oldValue~=nil, replaces, only if the entry's value equals oldValue.
Parameter | Type | Default | Description |
---|---|---|---|
key | keyType | number or string | |
oldValue | valueType | nil | A value to replace only if equals, nil to replace without equality check. |
newValue | valueType | The new value |
- Return
- int: 0 if not replaced, 1 if replaced.
- Errors
- if
type(key)
doesn't match constructor keyType parameter - if
type(oldValue)
ortype(newValue)
don't match constructor valueType parameter - if oldValue~=nil and values of this Map are not comparables
- Example
local m = Map:new("string", "number") m:putAll({Bach=1785, Mozart=1756}) -- Oops! wrong Bach birth date m:replace("Bach", nil, 1685) --> return 1, replaced without equality check m:replace("Mozart", 1757, 1234) --> return 0, Mozart value is 1756, not 1757 m:replace("Mozart", 1756, 1234) --> return 1
Map:values()
Returns a Collection view of the values contained in this Map, in the same order than the keys.
- Return
- Collection: A Collection that can contain multiple times the same value.
Map:ceilingEntry(keyType key)
Returns key and value associated with the least key greater than or equal to the given key, or nil,nil if there is no such key.
Parameter | Type | Default | Description |
---|---|---|---|
key | keyType | to search ceiling, number or string |
- Returns
- keyType: Key or
nil
if map is empty - valueType: Value, or
nil
if map is empty - Error
- if
type(key)
doesn't match constructor keyType parameter - See
- Map:ceilingKey
- Map:floorEntry
- Map:higherEntry
- Example
local m = Map:new("string","number") m:put("apple", 3); m:put("banana", 5); m:put("kiwi", 8) print(m:ceilingEntry("apricot")) --> banana 5 print(m:ceilingEntry("banana")) --> banana 5 print(m:ceilingEntry("pear")) --> nil nil
Map:ceilingKey(keyType key)
Returns the least key greater than or equal to the given key, or nil
if there is no such key.
Parameter | Type | Default | Description |
---|---|---|---|
key | keyType | Key to search for ceiling, number or string |
- Return
- keyType: A key or
nil
- Error
- if
type(key)
doesn't match constructor keyType parameter - See
- Map:ceilingEntry
- Map:floorKey
- Example
local m = Map:new("string","number") m:put("apple", 3); m:put("banana", 5); m:put("kiwi", 8) print(m:ceilingKey("apricot")) --> banana print(m:ceilingKey("banana")) --> banana print(m:ceilingKey("pear")) --> nil
Map:floorEntry(keyType key)
Returns key and mapped value associated with the greatest key less or equal than the given key, or nil,nil if there is no such key.
Parameter | Type | Default | Description |
---|---|---|---|
key | keyType | Key to search floor, number or string |
- Returns
- keyType: Key or
nil
if map is empty - valueType: Value, or
nil
if map is empty - Error
- if
type(key)
doesn't match constructor keyType parameter - See
- Map:floorKey
- Map:ceilingEntry
- Example
local m = Map:new("string","number") m:put("apple", 3); m:put("banana", 5); m:put("kiwi", 8) print(m:floorEntry("apricot")) --> apple 3 print(m:floorEntry("apple")) --> apple --> apple 3 print(m:floorEntry("pear")) --> kiwi 8 print(m:floorEntry("aaa")) -> nil nil
Map:floorKey(keyType key)
Returns the greatest key less or equal than the given key, or nil
if there is no such key.
Parameter | Type | Default | Description |
---|---|---|---|
key | keyType | Key to search for floor, number or string |
- Return
- keyType: A key or
nil
- Error
- if
type(key)
doesn't match constructor keyType parameter - See
- Map:floorEntry
- Map:ceilingKey
- Example
local m = Map:new("string","number") m:put("apple", 3); m:put("banana", 5); m:put("kiwi", 8) print(m:floorKey("apricot")) --> apple print(m:floorKey("apple")) --> apple print(m:floorKey("pear")) --> kiwi print(m:floorKey("aaa")) -> nil
Map:higherEntry(keyType key)
Returns entry (key,value) of the least key greater than the given key, or nil,nil if there is no such key.
Parameter | Type | Default | Description |
---|---|---|---|
key | keyType | Key to search higher, number or string |
- Returns
- keyType: Key or
nil
if map is empty - valueType: Value, or
nil
if map is empty - Error
- if
type(key)
doesn't match constructor keyType parameter - See
- Map:higherKey
- Map:lowerEntry
- Map:ceilingEntry
- Example
local m = Map:new("string","number") m:put("apple", 3); m:put("banana", 5); m:put("kiwi", 8) print(m:higherEntry("apricot")) --> banana 5 print(m:higherEntry("banana")) --> kiwi 8 print(m:higherEntry("pear")) --> nil nil
Map:higherKey(keyType key)
Returns the least key greater than the given key, or nil
if there is no such key.
Parameter | Type | Default | Description |
---|---|---|---|
key | keyType | Key to search for higher, number or string |
- Return
- keyType: Key or
nil
if map is empty - Error
- if
type(key)
doesn't match constructor keyType parameter - See
- Map:higherEntry
- Map:lowerKey
- Map:ceilingKey
- Example
local m = Map:new("string","number") m:put("apple", 3); m:put("banana", 5); m:put("kiwi", 8) print(m:higherKey("apricot")) --> banana print(m:higherKey("banana")) --> kiwi print(m:higherKey("pear")) --> nil
Map:lowerEntry(keyType key)
Returns key,value of the greatest key less than the given key, or nil,nil if there is no such key.
Parameter | Type | Default | Description |
---|---|---|---|
key | keyType | Key to search higher, number or string |
- Returns
- keyType: Key or
nil
if map is empty - valueType: Value, or
nil
if map is empty - Error
- if
type(key)
doesn't match constructor keyType parameter - See
- Map:lowerKey
- Map:higherEntry
- Map:floorEntry
- Example
local m = Map:new("string","number") m:put("apple", 3); m:put("banana", 5); m:put("kiwi", 8) print(m:lowerEntry("apricot")) --> apple 3 print(m:lowerEntry("banana")) --> apple 3 print(m:lowerEntry("apple")) --> nil nil
Map:lowerKey(keyType key)
Returns the greatest key less than the given key, or nil
if there is no such key.
Parameter | Type | Default | Description |
---|---|---|---|
key | keyType | Key to search higher, number or string |
- Return
- keyType: Key or
nil
if map is empty - Error
- if
type(key)
doesn't match constructor keyType parameter - See
- Map:lowerEntry
- Map:higherKey
- Map:floorKey
- Example
local m = Map:new("string","number") m:put("apple", 3); m:put("banana", 5); m:put("kiwi", 8) print(m:lowerKey("apricot")) --> apple print(m:lowerKey("banana")) --> apple print(m:lowerKey("apple")) --> nil
Map:firstEntry()
Returns the first (least if Map is sorted) entry (key and value) of this Map, or nil,nil if the Map is empty.
- Returns
- keyType: Key, or
nil
if map is empty - valueType: Value, or
nil
if map is empty - See
- Map:firstKey
- Map:lastEntry
- Example
local m = Map:new("string", "number") -- no sorting m:putAll({Vivaldi=1678, BAch=1685, Mozart=1756}) print(m:firstEntry()) --> Vivaldi 1678 m = Map:new("string", "number", SORT_ASCENDING) -- key are sorted alphabetically m:putAll({Vivaldi=1678, Bach=1685, Mozart=1756}) print(m:firstEntry()) --> Bach 1685
Map:firstKey()
Returns the first (least if Map is sorted) key of this Map, or nil
if the Map is empty.
- Return
- keyType: key, or
nil
if Map is empty - See
- Map:firstEntry
- Map:lastKey
- Example
local m = Map:new("string", "number") -- no sorting m:putAll({Vivaldi=1678, Bach=1685, Mozart=1756}) print(m:firstKey()) --> Vivaldi m = Map:new("string", "number", SORT_ASCENDING) -- key are sorted alphabetically m:putAll({Vivaldi=1678, Bach=1685, Mozart=1756}) print(m:firstKey()) --> Bach
Map:lastEntry()
Returns the last (greatest if Map is sorted) entry (key,value) of this Map, or nil,nil if this Map is empty.
- Returns
- keyType: Key, or
nil
if map is empty - valueType: Value, or
nil
if map is empty - See
- Map:lastKey
- Map:firstEntry
- Example
local m = Map:new("string", "number") -- no sorting m:putAll({Vivaldi=1678, Bach=1685, Mozart=1756}) print(m:lastEntry()) --> Mozart 1756 m = Map:new("string", "number", SORT_ASCENDING) -- key are sorted alphabetically m:putAll({Vivaldi=1678, Bach=1685, Mozart=1756}) print(m:lastEntry()) --> Vivaldi 1678
Map:lastKey()
Returns the last (greatest if Map is sorted) key of this Map, or nil
if Map is empty.
- Returns
- keyType: a key, or
nil
- keyType: Key, or
nil
if map is empty - valueType: Value, or
nil
if map is empty - See
- Map:lastEntry
- Map:firstKey
- Example
local m = Map:new("string", "number") -- no sorting m:putAll({Vivaldi=1678, Bach=1685, Mozart=1756}) print(m:lastKey()) --> Mozart m = Map:new("string", "number", SORT_ASCENDING) -- key are sorted alphabetically m:putAll({Vivaldi=1678, Bach=1685, Mozart=1756}) print(m:lastKey()) --> Vivaldi
Map:headMap(keyType toKey, boolean inclusive)
Returns a view of the portion of this map whose keys are less than (or equal to, if inclusive = true) toKey.
Parameter | Type | Default | Description |
---|---|---|---|
toKey | keyType | toKey | |
inclusive | boolean | false |
- Return
- Map: A Map, can be empty
- Error
- if
type(toKey)
doesn't match constructor keyType parameter - See
- Map:tailMap
- Map:subMap
- Example
local m = Map:new("string", "number") -- works even if map is unsorted m:putAll({Vivaldi=1678, Bach=1685, Mozart=1756}) local h = m:headMap("Bach", false) --> h is empty h = m:headMap("Bach", true) --> h contains Bach
Map:subMap(keyType fromKey, boolean fromInclusive, keyType toKey, boolean toInclusive)
Returns a view of the portion of this Map whose keys range from fromKey to toKey.
Parameter | Type | Default | Description |
---|---|---|---|
fromKey | keyType | fromKey | |
fromInclusive | boolean | true | Include fromKey in returned Map? |
toKey | keyType | toKey | |
toInclusive | boolean | false | Include toKey in returned Map? |
- Return
- Map: A new Map, can be empty
- Errors
- if
type(fromKey)
ortype(toKey)
doesn't match constructor keyType parameter - if toKey < fromKey
- See
- Map:headMap
- Map:tailMap
- Example
local m = Map:new("string", "number") -- work even if unsorted m:putAll({Vivaldi=1678, Bach=1685, Mozart=1756}) local s = m:subMap("Bach", false, "Purcell", false) --> s contains Mozart s = m:subMap("Bach", true, "Purcell", true) --> s contains Bach and Mozart s = m:subMap("Haydn", true, "Lully", true) --> s is empty
Map:tailMap(keyType fromKey, boolean inclusive)
Returns a view of the portion of this Map whose keys are greater than (or equal to, if inclusive==true) fromKey.
Parameter | Type | Default | Description |
---|---|---|---|
fromKey | keyType | fromKey | |
inclusive | boolean | false | Include fromKey in returned Map? |
- Return
- Map: A new Map, can be empty
- Error
- if
type(fromKey)
doesn't match constructor keyType parameter - See
- Map:headMap
- Map:subMap
- Example
local m = Map:new("string", "number") -- works even if unsorted m:putAll({Vivaldi=1678, Bach=1685, Mozart=1756}) local t = m:tailMap("Bach", false) --> t contains Mozart and Vivaldi t = m:tailMap("Vivaldi", false) --> t is empty t = m:tailMap("Vivaldi", true) --> t contains Vivaldi
Map:searchKeys(string regexp, boolean caseInsensitive)
If the keys are strings, search by a regexp and return a Collection of results.
Parameter | Type | Default | Description |
---|---|---|---|
regexp | string | regexp | |
caseInsensitive | boolean | false | Case insensitive search? Strings are strlower()ed, so the regexp must search in lower case. |
- Return
- Collection: Empty if no result found
- Error
- if constructor keyType parameter is not
"string"
Map:searchValues(string regexp, boolean caseInsensitive)
If the values are strings, search by a regexp and return a Collection of results.
Parameter | Type | Default | Description |
---|---|---|---|
regexp | string | regexp | |
caseInsensitive | boolean | false | Case insensitive search? Strings are strlower()ed, so the regexp must search in lower case. |
- Return
- Collection: Empty if no result found
- Error
- if constructor valueType parameter is not
"string"
Map:searchEntries(string regexp, boolean caseInsensitive)
If keys or values are strings, search by a regexp and return a Map of results.
Parameter | Type | Default | Description |
---|---|---|---|
regexp | string | regexp | |
caseInsensitive | boolean | false | Case insensitive search? Strings are strlower()ed, so the regexp must search in lower case. |
- Return
- Map: Empty if no result found
- Error
- if constructor keyType and valueType parameter is not
"string"
Map:iterator(string iteratorName)
Starts or restarts an iterator on the Map.
An iterator is a convenient way to browse the Map from first to last entry with :hasNext()
and :next()
methods.
You can provide an iteratorName to run multiple iterators simultaneously, else the result would be hazardous.
Parameter | Type | Default | Description |
---|---|---|---|
iteratorName | string | nil |
Map:reverseIterator(string iteratorName)
Starts or restarts a reverse iterator on the Map.
A reverse iterator is a convenient way to browse the Map from last to first entry with :hasNext()
and :next()
methods.
You can provide an iteratorName to run multiple iterators simultaneously, else the result would be hazardous.
Parameter | Type | Default | Description |
---|---|---|---|
iteratorName | string | nil |
Map:hasNext(string iteratorName)
While iterating, is there a next entry?
You can provide an iteratorName to run multiple iterators simultaneously, else the result would be hazardous.
Parameter | Type | Default | Description |
---|---|---|---|
iteratorName | string | nil |
- Return
-
nil
(for false) if end of Map is reached,true
if there are still some entries to iterate - See
- Map:iterator
- Map:reverseIterator
- Map:next
Map:next(string iteratorName)
While iterating, returns next entry if :hasNext()
, nil,nil otherwise.
You can provide an iteratorName to run multiple iterators simultaneously, else the result would be hazardous.
Parameter | Type | Default | Description |
---|---|---|---|
iteratorName | string | nil |
- Return
- key,value or nil,nil
- See
- Map:iterator
- Map:reverseIterator
- Map:hasNext
Map:print()
Prints a debug structure of the Map in console, if you want it prints only if LOG_LEVEL = LOG_LEVEL_DEBUG
, use :debug()
instead.
Map:debug()
Debug a structure of this Map in console, only if LOG_LEVEL = LOG_LEVEL_DEBUG
.