HashMap
HashMap 数据结构实现
源码
用法
go
import (
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
目录
API 文档
NewHashMap
新建默认容量(1 << 10)的HashMap指针实例
函数签名:
go
func NewHashMap() *HashMap
示例:
go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMap()
fmt.Println(hm)
}
NewHashMapWithCapacity
新建指定容量和长度的HashMap指针实例.
函数签名:
go
func NewHashMapWithCapacity(size, capacity uint64) *HashMap
示例:
go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMapWithCapacity(uint64(100), uint64(1000))
fmt.Println(hm)
}
Get
在hashmap中根据key获取值
函数签名:
go
func (hm *HashMap) Get(key any) any
示例:
go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMap()
val := hm.Get("a")
fmt.Println(val) //nil
}
Put
将key-value放入hashmap中
函数签名:
go
func (hm *HashMap) Put(key any, value any) any
示例:
go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMap()
hm.Put("a", 1)
val := hm.Get("a")
fmt.Println(val) //1
}
Delete
将指定的key从hashmap中删除
函数签名:
go
func (hm *HashMap) Delete(key any)
示例:
go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMap()
hm.Put("a", 1)
val := hm.Get("a")
fmt.Println(val) //1
hm.Delete("a")
val = hm.Get("a")
fmt.Println(val) //nil
}
Contains
判断hashmap中是否包含指定的key
函数签名:
go
func (hm *HashMap) Contains(key any) bool
示例:
go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMap()
hm.Put("a", 1)
fmt.Println(hm.Contains("a")) //true
fmt.Println(hm.Contains("b")) //false
}
Iterate
迭代hashmap,对每个key和value执行iteratee函数
函数签名:
go
func (hm *HashMap) Iterate(iteratee func(key, value any))
示例:
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)
})
}
Keys
返回hashmap所有key的切片 (随机顺序)
函数签名:
go
func (hm *HashMap) Keys() []any
示例:
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"}
}
Values
返回hashmap所有值的切片 (随机顺序)。
函数签名:
go
func (hm *HashMap) Values() []any
示例:
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}
}
FilterByValue
返回一个过滤后的HashMap。 如果任何值与 perdicate 函数不匹配,则返回 nil,否则返回包含选定值的 HashMap。
函数签名:
go
func (hm *HashMap) FilterByValue(perdicate func(value any) bool) *HashMap
示例:
go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := hashmap.NewHashMap()
hm.Put("a", 1)
hm.Put("b", 2)
hm.Put("c", 3)
hm.Put("d", 4)
hm.Put("e", 5)
hm.Put("f", 6)
filteredHM := hm.FilterByValue(func(value any) bool {
return value.(int) == 1 || value.(int) == 3
})
fmt.Println(filteredHM.Size()) //2
}