Package com.github.tadukoo.util.map
Class MultiMap<K,V>
java.lang.Object
com.github.tadukoo.util.map.MultiMap<K,V>
- Type Parameters:
K
- The type of keys in this MultiMapV
- The type of values in this MultiMap
- Direct Known Subclasses:
HashMultiMap
,TreeMultiMap
This class relates keys to values, but allows for one key to reference multiple values.
The implementation of this class is essentially a
For the most part, it's the same methods that a Map has, but there are some unique to this to allow for more specific functionality.
The implementation of this class is essentially a
Map
of keys to ArrayLists
of values.
For the most part, it's the same methods that a Map has, but there are some unique to this to allow for more specific functionality.
- Since:
- Pre-Alpha
- Version:
- Alpha v.0.2
- Author:
- Logan Ferree (Tadukoo)
-
Field Summary
-
Constructor Summary
ConstructorDescriptionSets the backingMap
for this MultiMap.Sets the backingMap
for this MultiMap and callsputAll(Pair[])
for the given Pair entries. -
Method Summary
Modifier and TypeMethodDescriptionasMap()
Returns the underlyingMap
of this MultiMap.final void
clear()
Removes all of the key-value associations from this MultiMap.final boolean
containsKey
(K key) Returns true if this MultiMap contains a mapping for the specified key.final boolean
containsValue
(V value) Returns true if this MultiMap maps one or more keys to the specified value.boolean
Compares the given object with this MultiMap for equality.final void
forEach
(BiConsumer<? super K, ? super V> action) Performs the given action for each key-value association in this MultiMap until all associations have been processed or the action throws an exception.Returns the list of values to which the specified key is mapped, or null if this MultiMap contains no mappings for the key.final boolean
isEmpty()
Returns true if this map contains no key-value mappings.keySet()
Returns the Set of keys in this MultiMap.final int
Returns the number of keys currently in this MultiMap.final void
Associates the specified value with the specified key in this MultiMap.final void
Associates all of the key-value mappings from the given MultiMap into this MultiMap.final void
Puts all the given Pairs into this MultiMap.final void
Associates all of the key-value mappings from the given Map into this MultiMap.final void
Associates all of the given values with the given key.final boolean
Removes the specified value from being associated with the specified key, if it exists.final boolean
removeEntireList
(K key, List<V> values) Removes the given list of values associated with the given key if the list of values matches the current list.Removes all values associated with the given key from the MultiMap.final boolean
Replaces the given old value with the given new value for an association to the given key, if the old value is currently associated with the given key.replaceEntireList
(K key, List<V> values) Replaces the current list of values associated with the given key with the given list of values, only if it is currently mapped to a value.final boolean
Replaces the current list of values associated with the given key with the given new list of values if the given old list matches the current list.final int
size()
Returns the number of key-value associations of this MultiMap.values()
Returns the list of values in this MultiMap as a single List.
-
Field Details
-
theMap
Underlying Map used to store the key-value pairs
-
-
Constructor Details
-
MultiMap
Sets the backingMap
for this MultiMap.- Parameters:
theMap
- The Map to use for this MultiMap
-
MultiMap
Sets the backingMap
for this MultiMap and callsputAll(Pair[])
for the given Pair entries.- Parameters:
theMap
- The Map to use for this MultiMapentries
- A collection of Pairs to be put in this MultiMap
-
MultiMap
- Parameters:
theMap
- The Map to use for this MultiMapotherMap
- A Map containing entries to add to this MultiMap
-
MultiMap
- Parameters:
theMap
- The Map to use for this MultiMapmultiMap
- A MultiMap containing entries to add to this MultiMap
-
-
Method Details
-
isEmpty
public final boolean isEmpty()Returns true if this map contains no key-value mappings. CallsMap.isEmpty()
on the underlyingMap
.- Returns:
- true if this map contains no key-value mappings.
-
equals
Compares the given object with this MultiMap for equality. Returns true if the given object is also a MultiMap and the two MultiMaps represent the same mappings. If they're both MultiMaps, it will run theasMap()
method on both MultiMaps and callMap.equals(java.lang.Object)
to compare their mappings. -
containsKey
Returns true if this MultiMap contains a mapping for the specified key. CallsMap.containsKey(java.lang.Object)
on the underlyingMap
to see if it contains the given key or not.- Parameters:
key
- The key to check for- Returns:
- Whether this MultiMap contains the given key or not
-
containsValue
Returns true if this MultiMap maps one or more keys to the specified value. Callsvalues()
and checks if the result contains the given value or not.- Parameters:
value
- The value to check for- Returns:
- Whether this MultiMap contains the given value or not
-
get
Returns the list of values to which the specified key is mapped, or null if this MultiMap contains no mappings for the key. CallsMap.get(java.lang.Object)
on the underlyingMap
.- Parameters:
key
- The key whose mapped values are being requested- Returns:
- The list of values mapped to the given key
-
keySet
Returns the Set of keys in this MultiMap. CallsMap.keySet()
on the underlyingMap
.- Returns:
- The Set of keys in this MultiMap
-
values
Returns the list of values in this MultiMap as a single List. CallsMap.values()
on the underlyingMap
and streams the Collection of Lists to a single List.- Returns:
- A list of all the values in this MultiMap
-
asMap
Returns the underlyingMap
of this MultiMap.- Returns:
- The underlying Map
-
put
Associates the specified value with the specified key in this MultiMap. Will create a newArrayList
with the given value if no List currently exists for the given key in this MultiMap, or add to the existing one if it already exists.- Parameters:
key
- The key to associate with the given valuevalue
- The value to associate with the given key
-
putAll
Associates all of the given values with the given key.- Parameters:
key
- The key to associate with the given valuesvalues
- The values to associate with the given key
-
putAll
Puts all the given Pairs into this MultiMap.- Parameters:
entries
- The entries to put in this MultiMap
-
putAll
Associates all of the key-value mappings from the given Map into this MultiMap.- Parameters:
map
- The map whose mappings should be added to this
-
putAll
Associates all of the key-value mappings from the given MultiMap into this MultiMap.- Parameters:
map
- The MultiMap whose mappings should be added to this one
-
remove
Removes the specified value from being associated with the specified key, if it exists.
Will return whether the mapping existed or not.
If the removed value was the last one associated with the given key, this method will remove the empty list from the underlyingMap
.- Parameters:
key
- The key of the given key-value association to removevalue
- The value of the given key-value association to remove- Returns:
- true if there was a mapping of the given key to the given value
-
removeKey
Removes all values associated with the given key from the MultiMap. CallsMap.remove(Object)
on the underlyingMap
.- Parameters:
key
- The key whose associations are to be removed- Returns:
- The List of values the key used to be associated with
-
removeEntireList
Removes the given list of values associated with the given key if the list of values matches the current list.
This method is callingMap.remove(Object,Object)
on the underlyingMap
.- Parameters:
key
- The key of the given key-value association to removevalues
- The values of the given key-value association to remove- Returns:
- Whether the list of values was removed or not
-
replace
Replaces the given old value with the given new value for an association to the given key, if the old value is currently associated with the given key.
Checks if there is an association between the given key and given old value, and if so, replaces it with the given new value. The new value is placed at the end of the backing List for the given key.- Parameters:
key
- The key to change the association ofoldValue
- The old value associated with the given keynewValue
- The new value to associate with the given key- Returns:
- true if the value was replaced
-
replaceEntireList
Replaces the current list of values associated with the given key with the given new list of values if the given old list matches the current list.
CallsMap.replace(Object, Object, Object)
on the underlyingMap
.- Parameters:
key
- The key to change the associations ofoldValues
- The old list of values associated with the given keynewValues
- The values to associate with the given key- Returns:
- true if the values were replaced
-
replaceEntireList
Replaces the current list of values associated with the given key with the given list of values, only if it is currently mapped to a value.
CallsMap.replace(Object, Object)
on the underlyingMap
.- Parameters:
key
- The key to change the associations ofvalues
- The values to associate with the given key- Returns:
- The previous list of values associated with the given key
-
keySetSize
public final int keySetSize()- Returns:
- The number of keys currently in this MultiMap
-
size
public final int size()Returns the number of key-value associations of this MultiMap.
Callsvalues()
to get the full list of values and returns the size of it.- Returns:
- The number of key-value associations of this MultiMap.
-
forEach
Performs the given action for each key-value association in this MultiMap until all associations have been processed or the action throws an exception.- Parameters:
action
- The action to be performed for each key-value association
-
clear
public final void clear()Removes all of the key-value associations from this MultiMap. The MultiMap will be empty after this call returns.
CallsMap.clear()
on the underlyingMap
.
-