Map
class
ContentProvider.get("Utils.Map");
An object that maps keys to values. Cannot contain duplicate keys, and one key is mapped to one value. Values and keys can be of different types.
Map is basically a wrapper for standard lua table, but provides some additional utility methods and changes to iteration in loops to simplify code and remove pairs()
boilerplate.
Returns | Constructor | Description |
---|---|---|
Map | Map() | Creates empty Map. |
Map | Map(table) | Creates Map with values from standard Lua table. |
Returns | Method | Description |
---|---|---|
void | clear() | Removes all mappings present in the Map. |
void | isEmpty() | Removes all mappings present in the Map. |
V? | compute(key, remappingFunction) | Modifies value of certain key with provided function. |
V? | computeIfAbsent(key, remappingFunction) | Assings value to certain key using provided function, but only if it is does not have value already assigned. |
V? | computeIfPresent(key, remappingFunction) | Assings value to certain key using provided function, but only if it already has value already assigned. |
boolean | containsKey(key) | Returns true if this map contains a mapping for the specified key. |
boolean | containsValue(value) | Returns true if this map maps one or more keys to the specified value. |
boolean | equals(map) | Tests if Map provided has same mappings as your map. |
void | forEach(action) | Performs the given action for each entry in this map. |
V? | get(key) | Returns the value to which the specified key is mapped. |
V? | put(key, value) | Associates the specified value with the specified key in this map. |
void | putAll(map) | Copies all of the mappings from the specified map to this map. |
V? | putIfAbsent(key, value) | If the key is not already mapped to a value mapps it to the given value. |
V? | remove(key) | Removes the mapping for a key from this map if it is present. |
boolean | remove(key, value) | Removes the mapping for a key from this map if it is matches value provided. |
boolean | replace(key, value) | Replaces entry only if it is already mapped to specific value. |
boolean | replaceAll(mappingFunction) | Replaces each entry’s value with the result of invoking the given function on that entry. |
table | getKeys() | Returns all keys that are mapped to values in this map. |
table | getValues() | Returns all values that are mapped to keys in this map. |
🔄 Iterating with Maps
Maps support iteration with pairs using
for
loops, orforEach
method.local map = Map(someTable) for key, value in map do print(string.format("%s : %s", key, value)) end --> key1 : value1 --> key2 : value2 --> key3 : value3
Map()
constructor
Return values
Map
- object created.
Map(table)
constructor
Returns Map
object with data from table
.
Parameters
table
standard Lua table in dictionary form.
Return values
Map
- object created.
clear()
Removes all mappings from the this map.
isEmpty()
Returns true if this map contains no key-value mappings.
Return values
boolean
- true if there are no mappings in this table
size()
Returns the number of key-value mappings in this map.
Return values
number
- amount of key-value mappings in this map
compute(key, mappingFunction<K, V>)
Attempts to compute a mapping for the specified key and its current mapped value (or nil if there is no current mapping).
Parameters
key
- key to compute mapping for.mappingFunction<K, V>
- function used to compute a mapping, accepts key and value, returns value.
Return values
V
- value computed.
computeIfAbsent(key, mappingFunction<K, V>)
If the specified key is not already associated with a value (or is mapped to nil), attempts to compute its value using the given mapping function and enters it into this map unless nil.
Parameters
key
- key to compute mapping for.mappingFunction<K>
- function used to compute a mapping, accepts only key.
Return values
V?
- value computed.
computeIfPresent(key, mappingFunction<K, V>)
If the value for the specified key is present, attempts to compute a new mapping given the key and its current mapped value.
Parameters
key
- key to compute mapping for.mappingFunction<K, V>
- function used to compute a mapping, accepts only key.
Return values
V?
- value computed.
containsKey(key)
Returns true if this map contains a mapping for the specified key.
Parameters
key
- key whose presence in this map is to be tested.
Return values
boolean
-true
if key is present,false
if it is missing.
containsValue(value)
Returns true if this map maps one or more keys to the specified value.
Parameters
value
- value whose presence in this map is to be tested.
Return values
boolean
-true
if this map maps one or more keys to the specified value.
equals(map)
Tests if Map provided has same mappings (key and value combinations) as your map.
Parameters
Map
- the map to check for equality.
Return values
boolean
- true if mappings are the same, false otherwise.
forEach(action)
Performs the given action for each entry in this map until all entries have been processed or the action errors.
-- Example for alternate way of iterating for you, JavaScript guys ☕️🗿
local map = Map(someTable)
map.forEach(function(key, value)
print(string.format("%s : %s", key, value))
end)
--> key1 : value1
--> key2 : value2
--> key3 : value3
Parameters
action<K, V>
- thefunction
that runs for every mapping in a map.
get(key)
Returns the value to which the specified key is mapped, or nil if this map contains no mapping for the key.
You can still use standard lua method of getting data from a table.
local map = Map(someTable); map.key1 = "value1"; print(map.key1); --> value1
Parameters
key
- the key whose associated value is to be returned.
Return values
V?
- the value to which the specified key is mapped, or null if this map contains no mapping for the key.
put(key, value)
Associates the specified value with the specified key in this map.
You can still use standard lua method of setting data to the table.
local map = Map(someTable); map.key1 = "value1"; print(map.key1); --> value1
Parameters
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key
Return values
V?
- the previous associated value with key, or nil if there was no previously associated value with a key.
putAll(map)
Copies all of the mappings from the specified map to this map.
Parameters
map
- the map to copy mappings from.
putIfAbsent(key, value)
If the specified key is not already associated with a value associates it with the given value and returns nil, else returns the current value.
Parameters
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key
Return values
V?
- the previous value associated with a key, or nil if there was no mapping for a key
remove(key)
Removes the mapping for a key from this map if it is present, if so - returns previous held value.
Parameters
key
- key with which the specified value is associated
Return values
V?
- the previous value associated with key, or null if there was no mapping for key
remove(key, value)
Removes the entry for the specified key only if it is currently mapped to the specified value.
Parameters
key
- key with which the specified value is associatedvalue
- value expected to be associated with the specified key
Return values
boolean
- true if the value was removed
replace(key, value)
Replaces entry only if it is already mapped to specific value, returns true if value was replaced
Parameters
key
- key with which the specified value is associatedvalue
- value expected to replace previous value with
Return values
boolean
- true if the value was replaced
replaceAll(mappingFunction<K, V>)
Replaces each entry’s value with the result of invoking the given function on that entry until all entries have been processed or function errored.
Parameters
mappingFunction<K, V>
- function used to be called to get new value, accepts key and value, returns value.
getKeys()
Returns all keys that are mapped to values in this map.
Return values
table
- contains all keys mapped to values
getValues()
Returns all values that are mapped to keys in this map.
Return values
table
- contains all values mapped to keys