Files
lancet/docs/datastructure/hashmap.md
2022-10-11 15:41:09 +08:00

4.6 KiB

HashMap

HashMap is a key value map data structure.

Source

Usage

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

Index

Documentation

NewHashMap

Make a HashMap instance with default capacity is 1 << 10.

Signature:

func NewHashMap() *HashMap

Example:

package main

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

func main() {
    hm := heap.NewHashMap()
    fmt.Println(hm)
}

NewHashMap

Make a HashMap instance with given size and capacity.

Signature:

func NewHashMapWithCapacity(size, capacity uint64) *HashMap

Example:

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

Get the value of given key in hashmap

Signature:

func (hm *HashMap) Get(key any) any

Example:

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

Put new key value in hashmap, then return value

Signature:

func (hm *HashMap) Put(key any, value any) any

Example:

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

Delete key-value item by given key in hashmap.

Signature:

func (hm *HashMap) Delete(key any)

Example:

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

Checks if given key is in hashmap or not.

Signature:

func (hm *HashMap) Contains(key any) bool

Example:

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

Executes iteratee funcation for every key and value pair of hashmap.

Signature:

func (hm *HashMap) Iterate(iteratee func(key, value any))

Example:

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

Return a slice of the hashmap's keys (random order).

Signature:

func (hm *HashMap) Keys() []any

Example:

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

Return a slice of the hashmap's values (random order).

Signature:

func (hm *HashMap) Values() []any

Example:

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