Documentation
¶
Overview ¶
mapx package provides enhanced map container apis. note not safety in concurrent operation.
Index ¶
- type Map
- func (m *Map[K, V]) Clear()
- func (m *Map[K, V]) Copy() *Map[K, V]
- func (m *Map[K, V]) Equals(mp *Map[K, V], eql base.EQL[V]) bool
- func (m *Map[K, V]) Exist(key K) bool
- func (m *Map[K, V]) ExistValue(value V) (k K, exist bool)
- func (m *Map[K, V]) ExistValueWithComparator(value V, equal base.EQL[V]) (k K, exist bool)
- func (m *Map[K, V]) Get(key K) (V, bool)
- func (m *Map[K, V]) IsEmpty() (empty bool)
- func (m *Map[K, V]) Keys() []K
- func (m *Map[K, V]) MaxKey(compare base.CMP[K]) (key K, v V)
- func (m *Map[K, V]) MaxValue(compare base.CMP[V]) (key K, v V)
- func (m *Map[K, V]) MinKey(compare base.CMP[K]) (key K, v V)
- func (m *Map[K, V]) MinValue(compare base.CMP[V]) (key K, v V)
- func (m *Map[K, V]) Put(key K, value V) V
- func (m *Map[K, V]) Range(f base.BiFunc[bool, K, V])
- func (m *Map[K, V]) Remove(key K) bool
- func (m *Map[K, V]) Size() int
- func (m *Map[K, V]) ToMap() map[K]V
- func (m *Map[K, V]) Values() []V
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Map ¶
type Map[K comparable, V any] struct { // contains filtered or unexported fields }
Map is like a Go map[interface{}]interface{} but is provide more useful methods
func NewMap ¶
func NewMap[K comparable, V any]() *Map[K, V]
NewMap create a new Map
Example ¶
package main import ( "fmt" "strings" "github.com/jhunters/goassist/container/mapx" ) type MapPojo struct { Name string } func (mp *MapPojo) CompareTo(v *MapPojo) int { return strings.Compare(mp.Name, v.Name) } func newMapPojo(name string) *MapPojo { return &MapPojo{name} } func main() { mp := mapx.NewMap[string, *MapPojo]() mp.Put("key1", newMapPojo("hello")) mp.Put("key2", newMapPojo("world")) mp.Put("key3", newMapPojo("to")) mp.Put("key4", newMapPojo("you")) mp.Put("key5", newMapPojo("!")) v, exist := mp.Get("key3") fmt.Println(v.Name, exist) keys := mp.Keys() fmt.Println(len(keys)) key, v := mp.MaxKey(func(s1, s2 string) int { return strings.Compare(s1, s2) }) fmt.Println(key, v.Name) key, v = mp.MaxValue(func(mp1, mp2 *MapPojo) int { return mp1.CompareTo(mp2) }) fmt.Println(key, v.Name) key, exist = mp.ExistValue(newMapPojo("to")) fmt.Println(key, exist) exist = mp.Remove("key3") fmt.Println(exist) }
Output: to true 5 key5 ! key4 you key3 true true
func (*Map[K, V]) Equals ¶ added in v1.0.6
Equal test and return if all keys and values are some to
Example ¶
package main import ( "fmt" "github.com/jhunters/goassist/container/mapx" ) func main() { mp1 := mapx.NewMap[string, string]() mp1.Put("key1", "value1") mp2 := mapx.NewMap[string, string]() mp2.Put("key1", "value1") fmt.Println(mp1.Equals(mp2, func(s1, s2 string) bool { return s1 == s2 })) }
Output:
func (*Map[K, V]) Exist ¶
Exist return true if key exist
Example ¶
package main import ( "fmt" "github.com/jhunters/goassist/container/mapx" ) func main() { mp := mapx.NewMap[string, string]() mp.Put("key1", ("2")) mp.Put("key2", ("3")) mp.Put("key3", ("4")) mp.Put("key4", ("1")) mp.Put("key5", ("5")) b := mp.Exist("key2") fmt.Println(b) }
Output: true
func (*Map[K, V]) ExistValue ¶
ExistValue return true if value exist
Example ¶
package main import ( "fmt" "github.com/jhunters/goassist/container/mapx" ) func main() { mp := mapx.NewMap[string, string]() mp.Put("key1", ("2")) mp.Put("key2", ("3")) mp.Put("key3", ("4")) mp.Put("key4", ("1")) mp.Put("key5", ("5")) k, exist := mp.ExistValue("5") fmt.Println(k, exist) k, exist = mp.ExistValue("15") fmt.Println(k, exist) }
Output: key5 true false
func (*Map[K, V]) ExistValueWithComparator ¶
ExistValue return true if value exist
Example ¶
package main import ( "fmt" "github.com/jhunters/goassist/container/mapx" ) func main() { mp := mapx.NewMap[string, string]() mp.Put("key1", ("2")) mp.Put("key2", ("3")) mp.Put("key3", ("4")) mp.Put("key4", ("1")) mp.Put("key5", ("5")) k, exist := mp.ExistValueWithComparator("5", func(s1, s2 string) bool { return s1 == s2 }) fmt.Println(k, exist) k, exist = mp.ExistValueWithComparator("15", func(s1, s2 string) bool { return s1 == s2 }) fmt.Println(k, exist) }
Output: key5 true false
func (*Map[K, V]) IsEmpty ¶
IsEmpty return true if no keys
Example ¶
package main import ( "fmt" "github.com/jhunters/goassist/container/mapx" ) func main() { mp := mapx.NewMap[string, string]() fmt.Println(mp.IsEmpty()) }
Output: true
func (*Map[K, V]) Keys ¶
func (m *Map[K, V]) Keys() []K
Keys return all key as slice in map
Example ¶
package main import ( "fmt" "github.com/jhunters/goassist/container/mapx" ) func main() { mp := mapx.NewMap[string, string]() mp.Put("key1", ("2")) mp.Put("key2", ("3")) mp.Put("key3", ("4")) mp.Put("key4", ("1")) mp.Put("key5", ("5")) keys := mp.Keys() fmt.Println(len(keys)) }
Output: 5
func (*Map[K, V]) MaxKey ¶
MaxKey to return max key in the map
Example ¶
package main import ( "fmt" "strings" "github.com/jhunters/goassist/container/mapx" ) func main() { mp := mapx.NewMap[string, string]() mp.Put("key1", ("2")) mp.Put("key2", ("3")) mp.Put("key3", ("4")) mp.Put("key4", ("1")) mp.Put("key5", ("5")) k, v := mp.MaxKey(func(s1, s2 string) int { return strings.Compare(s1, s2) }) fmt.Println(k, v) }
Output: key5 5
func (*Map[K, V]) MaxValue ¶
MaxValue to return max value in the map
Example ¶
package main import ( "fmt" "strings" "github.com/jhunters/goassist/container/mapx" ) func main() { mp := mapx.NewMap[string, string]() mp.Put("key1", ("2")) mp.Put("key2", ("3")) mp.Put("key3", ("4")) mp.Put("key4", ("1")) mp.Put("key5", ("5")) k, v := mp.MaxValue(func(s1, s2 string) int { return strings.Compare(s1, s2) }) fmt.Println(k, v) }
Output: key5 5
func (*Map[K, V]) MinKey ¶
MinKey to return min key in the map
Example ¶
package main import ( "fmt" "strings" "github.com/jhunters/goassist/container/mapx" ) func main() { mp := mapx.NewMap[string, string]() mp.Put("key1", ("2")) mp.Put("key2", ("3")) mp.Put("key3", ("4")) mp.Put("key4", ("1")) mp.Put("key5", ("5")) k, v := mp.MinKey(func(s1, s2 string) int { return strings.Compare(s1, s2) }) fmt.Println(k, v) }
Output: key1 2
func (*Map[K, V]) MinValue ¶
MinValue to return min value in the map
Example ¶
package main import ( "fmt" "strings" "github.com/jhunters/goassist/container/mapx" ) func main() { mp := mapx.NewMap[string, string]() mp.Put("key1", ("2")) mp.Put("key2", ("3")) mp.Put("key3", ("4")) mp.Put("key4", ("1")) mp.Put("key5", ("5")) k, v := mp.MinValue(func(s1, s2 string) int { return strings.Compare(s1, s2) }) fmt.Println(k, v) }
Output: key4 1
func (*Map[K, V]) ToMap ¶
func (m *Map[K, V]) ToMap() map[K]V
ToMap convert key and value to origin map struct
func (*Map[K, V]) Values ¶
func (m *Map[K, V]) Values() []V
Values return all value as slice in map
Example ¶
package main import ( "fmt" "github.com/jhunters/goassist/container/mapx" ) func main() { mp := mapx.NewMap[string, string]() mp.Put("key1", ("2")) mp.Put("key2", ("3")) mp.Put("key3", ("4")) mp.Put("key4", ("1")) mp.Put("key5", ("5")) vals := mp.Values() fmt.Println(len(vals)) }
Output: 5