Documentation ¶
Overview ¶
Example (NewHashMapFromBuiltinMap) ¶
lowLevelMap := map[string]int{ "a": 1, "b": 2, } var m Map[string, int] = NewHashMapFromBuiltinMap[map[string]int, string, int](lowLevelMap) fmt.Println(m.Size())
Output: 2
Index ¶
- func ComputeIfAbsent[M ~map[K]V, K comparable, V any](m M, key K, mapping func(k K) V)
- func ComputeIfPresent[M ~map[K]V, K comparable, V any](m M, key K, ...)
- func Copy[M ~map[K]V, K comparable, V any](src M) M
- func CopyTo[M ~map[K]V, K comparable, V any](src M, dst M) M
- func ForEach[M ~map[K]V, K comparable, V any](m M, consumer func(k K, v V))
- func ForEachIndexed[M ~map[K]V, K comparable, V any](m M, consumer func(index int, k K, v V) (stop bool))
- func GetOrDefault[M ~map[K]V, K comparable, V any](m M, key K, defaultValue V) V
- func Keys[M ~map[K]V, K comparable, V any](m M) []K
- func KeysAsStream[M ~map[K]V, K comparable, V any](m M) stream.Stream
- func MapAsStream[M ~map[K]V, K comparable, V any](m M) stream.Stream
- func MapString[K comparable, V any](m Map[K, V]) string
- func MarshalJSON[K comparable, V any](m Map[K, V]) (bz []byte, err error)
- func PutAll[M ~map[K]V, K comparable, V any](m M, other M)
- func RemoveIf[M ~map[K]V, K comparable, V any](m M, predicate func(key K, value V) bool)
- func UnmarshalJSON[K comparable, V any](m Map[K, V], data []byte) error
- func Values[M ~map[K]V, K comparable, V any](m M) []V
- func ValuesAsStream[M ~map[K]V, K comparable, V any](m M) stream.Stream
- type HashMap
- func (m HashMap[K, V]) AsBuiltinMap() map[K]V
- func (m HashMap[K, V]) Clear()
- func (m HashMap[K, V]) ComputeIfAbsent(key K, mapping func(key K) V)
- func (m HashMap[K, V]) ComputeIfPresent(key K, remapping func(key K, oldValue V) (newValue V, action RemappingAction))
- func (m HashMap[K, V]) ContainsKey(key K) bool
- func (m HashMap[K, V]) ForEach(consumer func(key K, value V))
- func (m HashMap[K, V]) ForEachIndexed(consumer func(index int, key K, value V) (stop bool))
- func (m HashMap[K, V]) Get(key K) (value V, found bool)
- func (m HashMap[K, V]) GetOrDefault(key K, defaultValue V) V
- func (m HashMap[K, V]) IsEmpty() bool
- func (m HashMap[K, V]) Keys() []K
- func (m HashMap[K, V]) MarshalJSON() ([]byte, error)
- func (m HashMap[K, V]) Put(key K, value V) (oldValue V, oldValueFound bool)
- func (m HashMap[K, V]) PutAll(other Map[K, V])
- func (m HashMap[K, V]) PutIfAbsent(key K, newValue V)
- func (m HashMap[K, V]) Remove(key K) (value V, found bool)
- func (m HashMap[K, V]) RemoveIf(predicate func(key K, value V) bool)
- func (m HashMap[K, V]) Size() int
- func (m HashMap[K, V]) String() string
- func (m HashMap[K, V]) UnmarshalJSON(bytes []byte) error
- func (m HashMap[K, V]) Values() []V
- type LinkedHashMap
- func (m *LinkedHashMap[K, V]) AsBuiltinMap() map[K]V
- func (m *LinkedHashMap[K, V]) Clear()
- func (m *LinkedHashMap[K, V]) ComputeIfAbsent(key K, mapping func(key K) V)
- func (m *LinkedHashMap[K, V]) ComputeIfPresent(key K, remapping func(key K, oldValue V) (newValue V, action RemappingAction))
- func (m *LinkedHashMap[K, V]) ContainsKey(key K) bool
- func (m *LinkedHashMap[K, V]) ForEach(consumer func(key K, value V))
- func (m *LinkedHashMap[K, V]) ForEachIndexed(consumer func(index int, key K, value V) (stop bool))
- func (m *LinkedHashMap[K, V]) Get(key K) (value V, found bool)
- func (m *LinkedHashMap[K, V]) GetOrDefault(key K, defaultValue V) V
- func (m *LinkedHashMap[K, V]) IsEmpty() bool
- func (m *LinkedHashMap[K, V]) Keys() []K
- func (m *LinkedHashMap[K, V]) MarshalJSON() ([]byte, error)
- func (m *LinkedHashMap[K, V]) Put(key K, value V) (oldValue V, oldValueFound bool)
- func (m *LinkedHashMap[K, V]) PutAll(other Map[K, V])
- func (m *LinkedHashMap[K, V]) PutIfAbsent(key K, newValue V)
- func (m *LinkedHashMap[K, V]) Remove(key K) (oldValue V, oldValueFound bool)
- func (m *LinkedHashMap[K, V]) RemoveIf(predicate func(key K, value V) bool)
- func (m *LinkedHashMap[K, V]) Size() int
- func (m *LinkedHashMap[K, V]) String() string
- func (m *LinkedHashMap[K, V]) UnmarshalJSON(bytes []byte) error
- func (m *LinkedHashMap[K, V]) Values() []V
- type Map
- type Pair
- type RemappingAction
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ComputeIfAbsent ¶
func ComputeIfAbsent[M ~map[K]V, K comparable, V any](m M, key K, mapping func(k K) V)
ComputeIfAbsent computes the value for the given key if it does not exist.
func ComputeIfPresent ¶
func ComputeIfPresent[M ~map[K]V, K comparable, V any]( m M, key K, remapping func(key K, oldValue V) (newValue V, action RemappingAction), )
ComputeIfPresent computes the value for the given key if it exists.
func CopyTo ¶
func CopyTo[M ~map[K]V, K comparable, V any](src M, dst M) M
CopyTo copies the map to the destination map.
func ForEach ¶
func ForEach[M ~map[K]V, K comparable, V any](m M, consumer func(k K, v V))
ForEach iterates over the map and calls the consumer function for each key-value pair.
func ForEachIndexed ¶
func ForEachIndexed[M ~map[K]V, K comparable, V any](m M, consumer func(index int, k K, v V) (stop bool))
ForEachIndexed iterates over the map and calls the consumer function for each key-value pair. The consumer function returns true to stop iterating.
func GetOrDefault ¶
func GetOrDefault[M ~map[K]V, K comparable, V any](m M, key K, defaultValue V) V
GetOrDefault returns the value for the given key if it exists, otherwise returns the default value.
func KeysAsStream ¶
func KeysAsStream[M ~map[K]V, K comparable, V any](m M) stream.Stream
KeysAsStream returns the keys of the map as a stream.
func MapAsStream ¶
func MapAsStream[M ~map[K]V, K comparable, V any](m M) stream.Stream
MapAsStream returns the map as a stream of key-value Pair.
func MapString ¶
func MapString[K comparable, V any](m Map[K, V]) string
MapString returns a string representation of the map.
func MarshalJSON ¶
func MarshalJSON[K comparable, V any](m Map[K, V]) (bz []byte, err error)
func PutAll ¶
func PutAll[M ~map[K]V, K comparable, V any](m M, other M)
PutAll adds all key-value pairs from the other map to this map.
func RemoveIf ¶
func RemoveIf[M ~map[K]V, K comparable, V any](m M, predicate func(key K, value V) bool)
RemoveIf removes all key-value pairs for which the predicate returns true.
func UnmarshalJSON ¶
func UnmarshalJSON[K comparable, V any](m Map[K, V], data []byte) error
func Values ¶
func Values[M ~map[K]V, K comparable, V any](m M) []V
Values returns the values of the map.
func ValuesAsStream ¶
func ValuesAsStream[M ~map[K]V, K comparable, V any](m M) stream.Stream
ValuesAsStream returns the values of the map as a stream.
Types ¶
type HashMap ¶
type HashMap[K comparable, V any] map[K]V
func NewHashMap ¶
func NewHashMap[K comparable, V any]() HashMap[K, V]
func NewHashMapFromBuiltinMap ¶
func NewHashMapFromBuiltinMap[M ~map[K]V, K comparable, V any](m M) HashMap[K, V]
NewHashMapFromBuiltinMap creates a new HashMap from a builtin map.
func NewHashMapFromMap ¶
func NewHashMapFromMap[K comparable, V any](m Map[K, V]) HashMap[K, V]
func NewHashMapWithSize ¶
func NewHashMapWithSize[K comparable, V any](size int) HashMap[K, V]
func (HashMap[K, V]) AsBuiltinMap ¶
func (m HashMap[K, V]) AsBuiltinMap() map[K]V
func (HashMap[K, V]) ComputeIfAbsent ¶
func (m HashMap[K, V]) ComputeIfAbsent(key K, mapping func(key K) V)
func (HashMap[K, V]) ComputeIfPresent ¶
func (m HashMap[K, V]) ComputeIfPresent(key K, remapping func(key K, oldValue V) (newValue V, action RemappingAction), )
func (HashMap[K, V]) ContainsKey ¶
func (HashMap[K, V]) ForEachIndexed ¶
func (HashMap[K, V]) GetOrDefault ¶
func (m HashMap[K, V]) GetOrDefault(key K, defaultValue V) V
func (HashMap[K, V]) MarshalJSON ¶
func (HashMap[K, V]) PutIfAbsent ¶
func (m HashMap[K, V]) PutIfAbsent(key K, newValue V)
func (HashMap[K, V]) UnmarshalJSON ¶
type LinkedHashMap ¶
type LinkedHashMap[K comparable, V any] struct { // contains filtered or unexported fields }
func NewLinkedHashMap ¶
func NewLinkedHashMap[K comparable, V any]() *LinkedHashMap[K, V]
func NewLinkedHashMapFromMap ¶
func NewLinkedHashMapFromMap[K comparable, V any](m Map[K, V]) *LinkedHashMap[K, V]
func NewLinkedHashMapWithSize ¶
func NewLinkedHashMapWithSize[K comparable, V any](size int) *LinkedHashMap[K, V]
func (*LinkedHashMap[K, V]) AsBuiltinMap ¶
func (m *LinkedHashMap[K, V]) AsBuiltinMap() map[K]V
func (*LinkedHashMap[K, V]) Clear ¶
func (m *LinkedHashMap[K, V]) Clear()
func (*LinkedHashMap[K, V]) ComputeIfAbsent ¶
func (m *LinkedHashMap[K, V]) ComputeIfAbsent(key K, mapping func(key K) V)
func (*LinkedHashMap[K, V]) ComputeIfPresent ¶
func (m *LinkedHashMap[K, V]) ComputeIfPresent(key K, remapping func(key K, oldValue V) (newValue V, action RemappingAction), )
func (*LinkedHashMap[K, V]) ContainsKey ¶
func (m *LinkedHashMap[K, V]) ContainsKey(key K) bool
func (*LinkedHashMap[K, V]) ForEach ¶
func (m *LinkedHashMap[K, V]) ForEach(consumer func(key K, value V))
func (*LinkedHashMap[K, V]) ForEachIndexed ¶
func (m *LinkedHashMap[K, V]) ForEachIndexed(consumer func(index int, key K, value V) (stop bool))
func (*LinkedHashMap[K, V]) Get ¶
func (m *LinkedHashMap[K, V]) Get(key K) (value V, found bool)
func (*LinkedHashMap[K, V]) GetOrDefault ¶
func (m *LinkedHashMap[K, V]) GetOrDefault(key K, defaultValue V) V
func (*LinkedHashMap[K, V]) IsEmpty ¶
func (m *LinkedHashMap[K, V]) IsEmpty() bool
func (*LinkedHashMap[K, V]) Keys ¶
func (m *LinkedHashMap[K, V]) Keys() []K
func (*LinkedHashMap[K, V]) MarshalJSON ¶
func (m *LinkedHashMap[K, V]) MarshalJSON() ([]byte, error)
func (*LinkedHashMap[K, V]) Put ¶
func (m *LinkedHashMap[K, V]) Put(key K, value V) (oldValue V, oldValueFound bool)
func (*LinkedHashMap[K, V]) PutAll ¶
func (m *LinkedHashMap[K, V]) PutAll(other Map[K, V])
func (*LinkedHashMap[K, V]) PutIfAbsent ¶
func (m *LinkedHashMap[K, V]) PutIfAbsent(key K, newValue V)
func (*LinkedHashMap[K, V]) Remove ¶
func (m *LinkedHashMap[K, V]) Remove(key K) (oldValue V, oldValueFound bool)
func (*LinkedHashMap[K, V]) RemoveIf ¶
func (m *LinkedHashMap[K, V]) RemoveIf(predicate func(key K, value V) bool)
func (*LinkedHashMap[K, V]) Size ¶
func (m *LinkedHashMap[K, V]) Size() int
func (*LinkedHashMap[K, V]) String ¶
func (m *LinkedHashMap[K, V]) String() string
func (*LinkedHashMap[K, V]) UnmarshalJSON ¶
func (m *LinkedHashMap[K, V]) UnmarshalJSON(bytes []byte) error
func (*LinkedHashMap[K, V]) Values ¶
func (m *LinkedHashMap[K, V]) Values() []V
type Map ¶
type Map[K comparable, V any] interface { fmt.Stringer json.Marshaler json.Unmarshaler // Put adds a key-value pair to the map. If the key already exists, the old value is replaced and returned. Put(key K, value V) (oldValue V, oldValueFound bool) // PutIfAbsent adds a key-value pair to the map if the key does not exist. PutIfAbsent(key K, newValue V) // PutAll adds all key-value pairs from the other map to this map. PutAll(other Map[K, V]) // ComputeIfAbsent computes the value for the given key if it does not exist. // If the key not exists, the value is computed and stored, otherwise the nothing happened. ComputeIfAbsent(key K, mapping func(key K) V) // ComputeIfPresent computes the value for the given key if it exists. ComputeIfPresent(key K, remapping func(key K, oldValue V) (newValue V, action RemappingAction)) // Get returns the value for the given key. If the key does not exist, the zero value is returned. Get(key K) (value V, found bool) // GetOrDefault returns the value for the given key. If the key does not exist, the default value is returned. GetOrDefault(key K, defaultValue V) V // ContainsKey returns true if the map contains the given key. ContainsKey(key K) bool // Keys returns the keys of the map. Keys() []K // Values returns the values of the map. Values() []V // ForEach iterates over all key-value pairs in the map. ForEach(consumer func(key K, value V)) // ForEachIndexed iterates over all key-value pairs in the map. // The consumer function returns true to stop iterating. ForEachIndexed(consumer func(index int, key K, value V) (stop bool)) // Remove removes the key-value pair with the given key from the map. If the key exists, the value is returned. Remove(key K) (value V, found bool) // RemoveIf removes all key-value pairs for which the predicate returns true. RemoveIf(predicate func(key K, value V) bool) // Clear removes all key-value pairs from the map. Clear() // IsEmpty returns true if the map is empty. IsEmpty() bool // Size returns the number of key-value pairs in the map. Size() int // AsBuiltinMap returns the map as a builtin map. // // The returned map is a copy of the map. AsBuiltinMap() map[K]V }
type RemappingAction ¶
type RemappingAction int
const ( // Replace the old value with the new value. Replace RemappingAction = iota // Remove the exists value. Remove // Noop does nothing. Noop )