Map
map<K, V> maps keys to values. Keys are unique, and a write over an existing key overwrites it. Read by index or with Get; the key set and the value set can each be taken out whole.
Creation and read/write
val m = new map<string, int>();
m.Set("a", 1);
m.Set("b", 2);
m["c"] = 3;
print(m, m.Count);
print(m["a"], m["b"]);
m["a"] = 10;
print(m);{a: 1, b: 2, c: 3} 3
1 2
{a: 10, b: 2, c: 3}Set(key, value) is equivalent to the indexer write m[key] = value; writing over an existing key replaces the old value.
Key access: m[key] is the assertion form — a missing key is a crash. Get(key) is the fallible form — a missing key fails with KeyNotFound.
Queries
print(m.Contains("a"), m.ContainsValue(3));
print(m.TryAdd("a", 99), m.TryAdd("d", 4), m);
m.Remove("b");
print(m);True True
False True {a: 10, b: 2, c: 3, d: 4}
{a: 10, c: 3, d: 4}Contains(key) checks keys, ContainsValue checks values. TryAdd(key, value) writes only when the key does not exist, returning a bool that reports whether the write happened. Remove(key) deletes a key-value pair, and Clear() empties the map.
Key set, value set, and entries
print(m.Keys(), m.Values());
print(m.Entries());[a, c, d] [10, 3, 4]
[(a, 10), (c, 3), (d, 4)]Keys() and Values() each copy one side into a list — use them when you only need one side. Entries() copies into a tuple list list<(K, V)>, where both destructuring and .Item members work (see tuples). Note that these three are methods and must be called with parentheses; Count remains a property.
Iteration
for-in over a map produces values; to get keys, iterate Keys():
for v in m
{
print("value", v);
}
for k in m.Keys()
{
print("key", k);
}value 10
value 3
value 4
key a
key c
key dIteration order matches insertion order. To take key and value together, iterate Entries() and destructure the tuples:
for (k, v) in m.Entries()
{
print(k, v);
}a 10
c 3
d 4Notes
- Printed form uses curly braces:
{a: 10, c: 3, d: 4}. Merge(other)merges in another map’s key-value pairs; on a key collision the argument side wins.Cloned()/DeepClone()copy the map.- Key types must be equatable; value types have no requirement. Keys hash by the default equality rule (structs by value, classes by identity) — see equality and hashing.
