Skip to content

HashMap

HashMap is a key value map data structure.

Source

Usage

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

Index

Documentation

NewHashMap

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

Signature:

go
func NewHashMap() *HashMap

Example:

go
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:

go
func NewHashMapWithCapacity(size, capacity uint64) *HashMap

Example:

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

Get the value of given key in hashmap

Signature:

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

Example:

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

Put new key value in hashmap, then return value

Signature:

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

Example:

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

Delete key-value item by given key in hashmap.

Signature:

go
func (hm *HashMap) Delete(key any)

Example:

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

Checks if given key is in hashmap or not.

Signature:

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

Example:

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

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

Signature:

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

Example:

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

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

Signature:

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

Example:

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

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

Signature:

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

Example:

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

Returns a filtered HashMap.

Signature:

go
func (hm *HashMap) FilterByValue(perdicate func(value any) bool) *HashMap

Example:

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
}

ToInterface

Converts reflect value to its interface type.

Signature:

go
func ToInterface(v reflect.Value) (value interface{}, ok bool)

Example:Run

go
package main

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

func main() {
    val := reflect.ValueOf("abc")
    iVal, ok := convertor.ToInterface(val)

    fmt.Printf("%T\n", iVal)
    fmt.Printf("%v\n", iVal)
    fmt.Println(ok)

    // Output:
    // string
    // abc
    // true    
}