1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 21:02:27 +08:00
Files
lancet/docs/datastructure/hashmap.md
2022-08-24 10:28:57 +08:00

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