mapx

package
v1.0.15 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

mapx package provides enhanced map container apis. note not safety in concurrent operation.

Index

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]) Clear

func (m *Map[K, V]) Clear()

Clear remove all key and value

func (*Map[K, V]) Copy

func (m *Map[K, V]) Copy() *Map[K, V]

Copy all keys and values to a new Map

func (*Map[K, V]) Equals added in v1.0.6

func (m *Map[K, V]) Equals(mp *Map[K, V], eql base.EQL[V]) bool

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

func (m *Map[K, V]) Exist(key K) bool

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

func (m *Map[K, V]) ExistValue(value V) (k K, exist bool)

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

func (m *Map[K, V]) ExistValueWithComparator(value V, equal base.EQL[V]) (k K, exist bool)

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]) Get

func (m *Map[K, V]) Get(key K) (V, bool)

Put put key and value to map

func (*Map[K, V]) IsEmpty

func (m *Map[K, V]) IsEmpty() (empty bool)

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

func (m *Map[K, V]) MaxKey(compare base.CMP[K]) (key K, v V)

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

func (m *Map[K, V]) MaxValue(compare base.CMP[V]) (key K, v V)

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

func (m *Map[K, V]) MinKey(compare base.CMP[K]) (key K, v V)

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

func (m *Map[K, V]) MinValue(compare base.CMP[V]) (key K, v V)

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]) Put

func (m *Map[K, V]) Put(key K, value V) V

Put put key and value to map

func (*Map[K, V]) Range

func (m *Map[K, V]) Range(f base.BiFunc[bool, K, V])

Range calls f sequentially for each key and value present in the map.

func (*Map[K, V]) Remove

func (m *Map[K, V]) Remove(key K) bool

Exist return true if key exist

func (*Map[K, V]) Size

func (m *Map[K, V]) Size() int

Size return count of size

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL