A map is a container that stores key-value pairs, also called entries. A map is sometimes called an associative array. Keys must implement a .hash
method that returns an Int
. A map can be built with entries between {
and }
.
Methods
at(key)
Return the value associated with key
. key
must be present.
def m = { 1: 'a', true: 3.14, "good": nil }
IO.println(m.at(true)) #> 3.14
See also: [ key ] operator
clear()
Remove all the elements from the map.
def m = { 1: 'a', true: 3.14, "good": nil }
m.clear()
IO.println(m) #> {}
clone()
Get a deep copy of the map. The keys and values of the map must implement the clone
method.
def m0 = { 1: 'a', true: 3.14, "good": nil }
def m1 = m0.clone()
IO.println(m1) #> {1: a, true: 3.14, good: nil}
contains(key)
Return true
if key
is present in the map.
def m = { 1: 'a', true: 3.14, "good": nil }
IO.println(m.contains(1)) #> true
IO.println(m.contains("bad")) #> false
erase(key)
Remove the entry whose key is key
. Return the value of the entry, or nil
if the key
was not present in the map.
def m = { 1: 'a', true: 3.14, "good": nil }
IO.println(m.erase(1)) #> a
IO.println(m.erase("bad")) #> nil
IO.println(m.size) #> 2
insert(key, value)
Insert an entry in the map. If key
is present in the map, the value is replaced with value
and the method returns false
. Otherwise, a new entry with key
and value
is created and inserted in the map and the method returns true
.
def m = { }
IO.println(m.insert(1, 'a')) #> true
IO.println(m.insert(1, 3.14)) #> false
IO.println(m) #> {1: 3.14}
See also: [ key ]=(value) operator
iterate(iterator), iterator_value(iterator)
Implement the iterator protocol to iterate over the entries of the map.
Each entry is wrapped in a MapEntry
object with .key
, .value
and .to_s
methods. The order of entries is not guaranteed.
def m = { 1: 'a' }
for (entry in m) {
IO.println(entry.type) #> MapEntry
IO.println(entry.key) #> 1
IO.println(entry.value) #> a
IO.println(entry) #> 1: a
}
keys
Return an iterable that can be used to iterate over the keys of the map. The order of the keys is not guaranteed.
def m = { 1: 'a' }
for (key in m.keys) {
IO.println(key) #> 1
}
See also: values
size
Return the number of entries in the map.
def m = { 1: 'a', true: 3.14, "good": nil }
IO.println(m.size) #> 3
to_s
Transform the map to a String
.
def m = { 1: 'a', true: 3.14, "good": nil }
IO.println(m.to_s) #> {1: a, true: 3.14, good: nil}
Operators
[ key ] operator
Return the value associated with key
if key
is present, or nil
otherwise.
def m = { 1: 'a', true: 3.14, "good": nil }
IO.println(m[true]) #> 3.14
IO.println(m["bad"]) #> nil
See also: [ key ]=(value) operator, at(key)
[ key ]=(value) operator
Associate value
to key
in the map. If key
is present in the map, the value is replaced with value
. Otherwise, a new entry with key
and value
is created. Return value
.
def m = { 1: 'a', true: 3.14, "good": nil }
IO.println(m[true] = 42.0) #> 42
IO.println(m[nil] = []) #> []
IO.println(m.size) #> 4
See also: [ key ] operator, insert(key, value)