mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-16 18:52:27 +08:00
doc: update document for hashmap
This commit is contained in:
@@ -29,6 +29,9 @@ import (
|
|||||||
- [Put](#Put)
|
- [Put](#Put)
|
||||||
- [Delete](#Delete)
|
- [Delete](#Delete)
|
||||||
- [Contains](#Contains)
|
- [Contains](#Contains)
|
||||||
|
- [Iterate](#Iterate)
|
||||||
|
- [Keys](#Keys)
|
||||||
|
- [Values](#Values)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -207,3 +210,105 @@ func main() {
|
|||||||
fmt.Println(hm.Contains("b")) //false
|
fmt.Println(hm.Contains("b")) //false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Iterate">Iterate</span>
|
||||||
|
|
||||||
|
<p>Executes iteratee funcation for every key and value pair of hashmap.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Iterate(iteratee func(key, value any))
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMap()
|
||||||
|
hm.Put("a", 1)
|
||||||
|
hm.Put("b", 2)
|
||||||
|
hm.Put("c", 3)
|
||||||
|
|
||||||
|
hm.Iterate(func(key, value any) {
|
||||||
|
fmt.Println(key)
|
||||||
|
fmt.Println(value)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Keys">Keys</span>
|
||||||
|
|
||||||
|
<p>Return a slice of the hashmap's keys (random order).</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Keys() []any
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMap()
|
||||||
|
hm.Put("a", 1)
|
||||||
|
hm.Put("b", 2)
|
||||||
|
hm.Put("c", 3)
|
||||||
|
|
||||||
|
keys := hm.Keys()
|
||||||
|
fmt.Println(keys) //[]interface{"a", "b", "c"}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Values">Values</span>
|
||||||
|
|
||||||
|
<p>Return a slice of the hashmap's values (random order).</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Values() []any
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMap()
|
||||||
|
hm.Put("a", 1)
|
||||||
|
hm.Put("b", 2)
|
||||||
|
hm.Put("c", 3)
|
||||||
|
|
||||||
|
values := hm.Values()
|
||||||
|
fmt.Println(values) //[]interface{2, 1, 3}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -24,11 +24,13 @@ import (
|
|||||||
|
|
||||||
- [NewHashMap](#NewHashMap)
|
- [NewHashMap](#NewHashMap)
|
||||||
- [NewHashMapWithCapacity](#NewHashMapWithCapacity)
|
- [NewHashMapWithCapacity](#NewHashMapWithCapacity)
|
||||||
|
|
||||||
- [Get](#Get)
|
- [Get](#Get)
|
||||||
- [Put](#Put)
|
- [Put](#Put)
|
||||||
- [Delete](#Delete)
|
- [Delete](#Delete)
|
||||||
- [Contains](#Contains)
|
- [Contains](#Contains)
|
||||||
|
- [Iterate](#Iterate)
|
||||||
|
- [Keys](#Keys)
|
||||||
|
- [Values](#Values)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -203,3 +205,104 @@ func main() {
|
|||||||
fmt.Println(hm.Contains("b")) //false
|
fmt.Println(hm.Contains("b")) //false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Iterate">Iterate</span>
|
||||||
|
|
||||||
|
<p>迭代hashmap,对每个key和value执行iteratee函数</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Iterate(iteratee func(key, value any))
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMap()
|
||||||
|
hm.Put("a", 1)
|
||||||
|
hm.Put("b", 2)
|
||||||
|
hm.Put("c", 3)
|
||||||
|
|
||||||
|
hm.Iterate(func(key, value any) {
|
||||||
|
fmt.Println(key)
|
||||||
|
fmt.Println(value)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Keys">Keys</span>
|
||||||
|
|
||||||
|
<p>返回hashmap所有key的切片 (随机顺序)</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Keys() []any
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMap()
|
||||||
|
hm.Put("a", 1)
|
||||||
|
hm.Put("b", 2)
|
||||||
|
hm.Put("c", 3)
|
||||||
|
|
||||||
|
keys := hm.Keys()
|
||||||
|
fmt.Println(keys) //[]interface{"a", "b", "c"}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Values">Values</span>
|
||||||
|
|
||||||
|
<p>返回hashmap所有值的切片 (随机顺序).</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Values() []any
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMap()
|
||||||
|
hm.Put("a", 1)
|
||||||
|
hm.Put("b", 2)
|
||||||
|
hm.Put("c", 3)
|
||||||
|
|
||||||
|
values := hm.Values()
|
||||||
|
fmt.Println(values) //[]interface{2, 1, 3}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user