import{_ as p,o as l,c as o,k as s,a as n,X as a}from"./chunks/framework.6e839c56.js";const D=JSON.parse('{"title":"HashMap","description":"","frontmatter":{},"headers":[],"relativePath":"en/api/packages/datastructure/hashmap.md","filePath":"en/api/packages/datastructure/hashmap.md"}'),e={name:"en/api/packages/datastructure/hashmap.md"},t=s("h1",{id:"HashMap",tabindex:"-1"},[n("HashMap "),s("a",{class:"header-anchor",href:"#HashMap","aria-label":'Permalink to "HashMap"'},"")],-1),c=s("p",null,"HashMap is a key value map data structure.",-1),r=s("div",{STYLE:"page-break-after: always;"},null,-1),y=s("h2",{id:"Source",tabindex:"-1"},[n("Source "),s("a",{class:"header-anchor",href:"#Source","aria-label":'Permalink to "Source"'},"")],-1),F=s("ul",null,[s("li",null,[s("a",{href:"https://github.com/duke-git/lancet/blob/main/datastructure/hashmap/hashmap.go",target:"_blank",rel:"noreferrer"},"https://github.com/duke-git/lancet/blob/main/datastructure/hashmap/hashmap.go")])],-1),i=s("div",{STYLE:"page-break-after: always;"},null,-1),u=a(`
import (
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)import (
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)Make a HashMap instance with default capacity is 1 << 10.
Signature:
func NewHashMap() *HashMapfunc NewHashMap() *HashMapExample:
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)
}Make a HashMap instance with given size and capacity.
Signature:
func NewHashMapWithCapacity(size, capacity uint64) *HashMapfunc NewHashMapWithCapacity(size, capacity uint64) *HashMapExample:
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 the value of given key in hashmap
Signature:
func (hm *HashMap) Get(key any) anyfunc (hm *HashMap) Get(key any) anyExample:
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 new key value in hashmap, then return value
Signature:
func (hm *HashMap) Put(key any, value any) anyfunc (hm *HashMap) Put(key any, value any) anyExample:
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-value item by given key in hashmap.
Signature:
func (hm *HashMap) Delete(key any)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
}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
}Checks if given key is in hashmap or not.
Signature:
func (hm *HashMap) Contains(key any) boolfunc (hm *HashMap) Contains(key any) boolExample:
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
}Executes iteratee funcation for every key and value pair of hashmap.
Signature:
func (hm *HashMap) Iterate(iteratee func(key, value any))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)
})
}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)
})
}Return a slice of the hashmap's keys (random order).
Signature:
func (hm *HashMap) Keys() []anyfunc (hm *HashMap) Keys() []anyExample:
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"}
}Return a slice of the hashmap's values (random order).
Signature:
func (hm *HashMap) Values() []anyfunc (hm *HashMap) Values() []anyExample:
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}
}