moonbit |
---|
true |
A mutable linked hash map based on a Robin Hood hash table, links all entry nodes in a linked list to maintains the order of insertion.
You can create an empty map using new()
or construct it using from_array()
.
let _map1 : Map[String, Int] = {}
let _map2 = { "one" : 1 , "two" : 2, "three" : 3, "four" : 4, "five" : 5}
You can use set()
to add a key-value pair to the map, and use get()
to get a key-value pair.
let map : Map[String, Int] = {}
map.set("a", 1)
assert_eq!(map.get("a"), Some(1))
assert_eq!(map.get_or_default("a", 0), 1)
assert_eq!(map.get_or_default("b", 0), 0)
You can use remove()
to remove a key-value pair.
let map = {"a" : 1, "b" : 2, "c" : 3}
map.remove("a")
You can use contains()
to check whether a key exists.
let map = {"a" : 1, "b" : 2, "c" : 3}
assert_eq!(map.contains("a"), true)
assert_eq!(map.contains("d"), false)
You can use size()
to get the number of key-value pairs in the map, or capacity()
to get the current capacity.
let map = {"a" : 1, "b": 2, "c": 3}
assert_eq!(map.size(), 3)
assert_eq!(map.capacity(), 8)
Similarly, you can use is_empty()
to check whether the map is empty.
let map : Map[String, Int] = {}
assert_eq!(map.is_empty(), true)
You can use clear
to remove all key-value pairs from the map, but the allocated memory will not change.
let map = {"a": 1, "b": 2, "c": 3}
map.clear()
assert_eq!(map.is_empty(), true)
You can use each()
or eachi()
to iterate through all key-value pairs in the order of insertion.
let map = {"a": 1, "b": 2, "c": 3}
let arr = []
map.each(fn(k, v) { arr.push((k, v)) })
assert_eq!(arr, [("a", 1), ("b", 2), ("c", 3)])
let arr2 = []
map.eachi(fn(i, k, v) { arr2.push((i, k, v)) })
assert_eq!(arr2, [(0, "a", 1), (1, "b", 2), (2, "c", 3)])