Skip to content
本页内容

# HashMap ​

HashMap 数据结构实现

## 源码 ​

- https://github.com/duke-git/lancet/blob/main/datastructure/hashmap/hashmap.go

## 用法 ​

go

    import (
        hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
    )

    import (
        hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
    )

## 目录 ​

- NewHashMap
- NewHashMapWithCapacity
- Get
- Put
- Delete
- Contains
- Iterate
- Keys
- Values

## API 文档 ​

### NewHashMap ​

新建默认容量（1 << 10）的HashMap指针实例

函数签名:

go

    func NewHashMap() *HashMap

    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)
    }

    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

    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)
    }

    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

    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
    }

    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

    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
    }

    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)

    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
    }

    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

    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
    }

    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))

    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)
        })
    }

    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

    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"}
    }

    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

    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}
    }

    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}
    }