diff --git a/builtin/LinkedHashMap.md b/builtin/LinkedHashMap.md index d154d09fb..e72b4ea89 100644 --- a/builtin/LinkedHashMap.md +++ b/builtin/LinkedHashMap.md @@ -11,8 +11,8 @@ A mutable linked hash map based on a Robin Hood hash table, links all entry node You can create an empty map using `new()` or construct it using `from_array()`. ```moonbit -let map1 : Map[String, Int] = Map::new() -let map2 = Map::[("one", 1), ("two", 2), ("three", 3), ("four", 4), ("five", 5)] +let map1 : Map[String, Int] = {} +let map2 = { "one" : 1 , "two" : 2, "three" : 3, "four" : 4, "five" : 5} ``` ### Set & Get @@ -20,7 +20,7 @@ let map2 = Map::[("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. ```moonbit -let map : Map[String, Int] = Map::new() +let map : Map[String, Int] = {} map.set("a", 1) map.get("a") // 1 map.get_or_default("a", 0) // 1 @@ -32,7 +32,7 @@ map.get_or_default("b", 0) // 0 You can use `remove()` to remove a key-value pair. ```moonbit -let map = Map::[("a", 1), ("b", 2), ("c", 3)] +let map = {"a" : 1, "b" : 2, "c" : 3} map.remove("a") ``` @@ -41,7 +41,7 @@ map.remove("a") You can use `contains()` to check whether a key exists. ```moonbit -let map = Map::[("a", 1), ("b", 2), ("c", 3)] +let map = {"a" : 1, "b" : 2, "c" : 3} map.contains("a") // true map.contains("d") // false ``` @@ -51,7 +51,7 @@ 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. ```moonbit -let map = Map::[("a", 1), ("b", 2), ("c", 3)] +let map = {"a" : 1, "b": 2, "c": 3} map.size() // 3 map.capacity() // 8 ``` @@ -59,7 +59,7 @@ map.capacity() // 8 Similarly, you can use `is_empty()` to check whether the map is empty. ```moonbit -let map : Map[String, Int] = Map::new() +let map : Map[String, Int] = {} map.is_empty() // true ``` @@ -68,7 +68,7 @@ map.is_empty() // true You can use `clear` to remove all key-value pairs from the map, but the allocated memory will not change. ```moonbit -let map = Map::[("a", 1), ("b", 2), ("c", 3)] +let map = {"a": 1, "b": 2, "c": 3} map.clear() map.is_empty() // true ``` @@ -78,7 +78,7 @@ map.is_empty() // true You can use `iter()` or `iteri()` to iterate through all key-value pairs in the order of insertion. ```moonbit -let map = Map::[("a", 1), ("b", 2), ("c", 3)] +let map = {"a": 1, "b": 2, "c": 3} map.iter(fn(k, v) { println("\(k)-\(v)") }) map.iteri(fn(i, k, v) { println("\(i)-\(k)-\(v)") }) ``` diff --git a/builtin/linkedhashmap.mbt b/builtin/linkedhashmap.mbt index 23fc46d19..02e76a5a2 100644 --- a/builtin/linkedhashmap.mbt +++ b/builtin/linkedhashmap.mbt @@ -35,7 +35,7 @@ priv struct ListNode[K, V] { /// # Example /// /// ``` -/// let map = HashMap::[(3, "three"), (8, "eight"), (1, "one")] +/// let map = { 3: "three", 8 : "eight", 1 : "one"} /// println(map.get(2)) // output: None /// println(map.get(3)) // output: Some("three") /// map.set(3, "updated")