Class std::Map<KT, VT>

Implements Iterable<(KT, VT)>

The Map class implements a mapping from keys to values. Keys may be any objects that provide a hash value using the std::Hash function and support the == operator, while values may be arbitrary objects. At most a single object can be associated with a specific key. If several values are assigned to a key, the later assignments replace the earlier values.

class Map(... as Pair<KT, VT>)
Construct a Map object. The constructor accepts an arbitrary number of Pair objects that define the initial (key, value) pairs to be added to the map. The left item of each pair is the key, and the right item is the value. Example:
var m = Map("one": 1, "three": 33)
m["one"]   -- Result: 1

Map methods

get(key as KT, default as VT) as VT
Return the value associated with key if the key is in the map. Otherwise, return default.
length() as Int
Return the number of keys in a map.
hasKey(key as Object) as Boolean
Return True if the key is in the map, or False otherwise.
remove(key as KT)
Remove a key and the corresponding value from the map. Raise KeyError if the key is not in the map.
keys() as Array<KT>
Return an array of all the keys in the map. The keys are returned in an arbitrary order.
values() as Array<VT>
Return an array of all the values in the map (one per key). The values are returned in an arbitrary order.
items() as Array<(KT, VT)>
Return an array of all the (key, value) pairs in the map. Each pair is represented as a tuple. The items are returned in an arbitrary order.
iterator() as Iterator<(KT, VT)>
Return an iterator object that returns all the (key, value) pairs in the map, in an arbitrary order. Each item in the iteration is a tuple with two items, key and value.

Note: You should not modify the map object during iteration, as the iterator assumes that the items in the map remain fixed.

Map operations

Map objects support the following operations:

map[key] (Map<KT, VT>[KT] ⇒ VT)
Return the value associated with a key, or raise KeyError if the key is not in the map.
map[key] = value (Map<KT, VT>[KT] = VT)
Add a new association to the map, or replace an existing association, if the key already exists.
for key, value in map (for KT, VT in Map<KT, VT>)
Map contents can be iterated with a for loop. Each item in the iteration is a tuple (key, value). The items are iterated in an arbitrary order.

Note: You should not modify the map object during iteration, as the iterator assumes that the items in the map remain fixed.

Str(map)
Return a string representation of a map.