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}
}
FilterByValue
Returns a filtered HashMap.
Signature:
func (hm *HashMap) FilterByValue(perdicate func(value any) bool) *HashMap
Example:
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:
func ToInterface(v reflect.Value) (value interface{}, ok bool)
Example:Run
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
}