Package com.github.tadukoo.util.map
Class ManyToManyMap<K,V>
java.lang.Object
com.github.tadukoo.util.map.ManyToManyMap<K,V>
- Type Parameters:
K
- The type of keys in this ManyToManyMapV
- The type of values in this ManyToManyMap
- Direct Known Subclasses:
HashManyToManyMap
,TreeManyToManyMap
This class relates keys to values, but allows for a many-to-many relationship of keys to values.
The implementation of this class is essentially two
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 two
MultiMap
s. One associates keys to values,
and the other associates values to keys.
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
ModifierConstructorDescriptionprotected
Sets the backingMultiMap
s for this ManyToManyMap.protected
ManyToManyMap
(MultiMap<K, V> keysToValues, MultiMap<V, K> valuesToKeys, ManyToManyMap<K, V> manyToManyMap) protected
protected
Sets the backingMultiMap
s for this ManyToManyMap and callsputAllKeyValMappings(Pair[])
for the given key-value Pairs.protected
-
Method Summary
Modifier and TypeMethodDescriptionfinal void
clear()
Removes all of the key-value associations from this ManyToManyMap.final boolean
containsKey
(K key) Returns true if this ManyToManyMap contains a mapping for the specified key.final boolean
containsValue
(V value) Returns true if this ManyToManyMap contains a mapping for the specified value.boolean
Compares the given object with this ManyToManyMap for equality.final void
forEach
(BiConsumer<? super K, ? super V> action) Performs the given action for each key-value association in this ManyToManyMap until all associations have been processed or the action throws an exception.Returns the list of keys to which the specified value is mapped, or null if this ManyToManyMap contains no mappings for the value.Returns the list of values to which the specified key is mapped, or null if this ManyToManyMap 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 ManyToManyMap.final int
Returns the number of keys currently in this ManyToManyMap.Returns the underlying keysToValuesMultiMap
of this ManyToManyMap.final void
Associates the specified value with the specified key in this ManyToManyMap.final void
putAllKeys
(V value, List<K> keys) Associates all of the given keys with the given value.final void
putAllKeyValMappings
(ManyToManyMap<K, V> map) Associates all of the key-value mappings from the given ManyToManyMap into this ManyToManyMap.final void
putAllKeyValMappings
(MultiMap<K, V> map) Associates all of the key-value mappings from the given MultiMap into this ManyToManyMap.final void
putAllKeyValMappings
(Pair<K, V>... entries) Puts all the given key-value Pairs into this ManyToManyMap.final void
putAllKeyValMappings
(Map<K, V> map) Associates all of the key-value mappings from the given Map into this ManyToManyMap.final void
putAllValKeyMappings
(ManyToManyMap<V, K> map) Associates all of the value-key mappings from the given ManyToManyMap into this ManyToManyMap.final void
putAllValKeyMappings
(MultiMap<V, K> map) Associates all of the value-key mappings from the given MultiMap into this ManyToManyMap.final void
putAllValKeyMappings
(Pair<V, K>... entries) Puts all the given value-key Pairs into this ManyToManyMap.final void
putAllValKeyMappings
(Map<V, K> map) Associates all of the value-key mappings from the given Map into this ManyToManyMap.final void
putAllValues
(K key, List<V> values) Associates all of the given values with the given key.final boolean
Removes the specified value from being associated with the specified key, if the mapping exists.final boolean
removeEntireKeysList
(V value, List<K> keys) Removes the given list of keys associated with the given value if the list of keys matches the current list associated with that value.final boolean
removeEntireValuesList
(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 associated with that key.Removes all values associated with the given key from the ManyToManyMap.removeValue
(V value) Removes all keys associated with the given value from the ManyToManyMap.replaceEntireKeyList
(V value, List<K> keys) Replaces the current list of keys associated with the given value with the given list of keys, only if it is currently mapped to a value.final boolean
Replaces the current list of keys associated with the given value with the given new list of keys if the given old list matches the current list.replaceEntireValueList
(K key, List<V> values) Replaces the current list of values associated with the given key with the given list of values.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 boolean
replaceKey
(V value, K oldKey, K newKey) Replaces the given old key with the given new key for an association to the given value, if the old key is currently associated with the given value.final boolean
replaceValue
(K key, V oldValue, V newValue) 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.final int
size()
Returns the number of key-value associations of this ManyToManyMap.valueSet()
Returns the Set of values in this ManyToManyMap.final int
Returns the number of values currently in this ManyToManyMap.Returns the underlying valuesToKeysMultiMap
of this ManyToManyMap.
-
Field Details
-
Constructor Details
-
ManyToManyMap
Sets the backingMultiMap
s for this ManyToManyMap.- Parameters:
keysToValues
- The keysToValues MultiMap to use for this ManyToManyMapvaluesToKeys
- The valuesToKeys MultiMap to use for this ManyToManyMap
-
ManyToManyMap
@SafeVarargs protected ManyToManyMap(MultiMap<K, V> keysToValues, MultiMap<V, K> valuesToKeys, Pair<K, V>... entries) Sets the backingMultiMap
s for this ManyToManyMap and callsputAllKeyValMappings(Pair[])
for the given key-value Pairs.- Parameters:
keysToValues
- The keysToValues MultiMap to use for this ManyToManyMapvaluesToKeys
- The valuesToKeys MultiMap to use for this ManyToManyMapentries
- A collection of key-value Pairs to be put in this ManyToManyMap
-
ManyToManyMap
- Parameters:
keysToValues
- The keysToValues MultiMap to use for this ManyToManyMapvaluesToKeys
- The valuesToKeys MultiMap to use for this ManyToManyMapmap
- A Map containing entries to add to this ManyToManyMap
-
ManyToManyMap
protected ManyToManyMap(MultiMap<K, V> keysToValues, MultiMap<V, K> valuesToKeys, MultiMap<K, V> multiMap) Sets the backingMultiMap
s for this ManyToManyMap and callsputAll
for the given MultiMap's entries.- Parameters:
keysToValues
- The keysToValues MultiMap to use for this ManyToManyMapvaluesToKeys
- The valuesToKeys MultiMap to use for this ManyToManyMapmultiMap
- A MultiMap containing entries to add to this ManyToManyMap
-
ManyToManyMap
protected ManyToManyMap(MultiMap<K, V> keysToValues, MultiMap<V, K> valuesToKeys, ManyToManyMap<K, V> manyToManyMap) Sets the backingMultiMap
s for this ManyToManyMap and callsputAll
for the given ManyToManyMap's entries.- Parameters:
keysToValues
- The keysToValues MultiMap to use for this ManyToManyMapvaluesToKeys
- The valuesToKeys MultiMap to use for this ManyToManyMapmanyToManyMap
- A ManyToManyMap containing entries to add to this ManyToManyMap
-
-
Method Details
-
isEmpty
public final boolean isEmpty()Returns true if this map contains no key-value mappings. CallsMultiMap.isEmpty()
on the underlying MultiMaps.- Returns:
- true if this map contains no key-value mappings.
-
equals
Compares the given object with this ManyToManyMap for equality. Returns true if the given object is also a ManyToManyMap and their underlyingMultiMaps
represent the same mappings. If they're both ManyToManyMaps, it will run theMultiMap.equals(java.lang.Object)
method on their underlying MultiMaps to compare their mappings. -
containsKey
Returns true if this ManyToManyMap contains a mapping for the specified key. CallsMultiMap.containsKey(K)
on the underlying keysToValues MultiMap to see if it contains the given key or not.- Parameters:
key
- The key to check for- Returns:
- Whether this ManyToManyMap contains the given key or not
-
containsValue
Returns true if this ManyToManyMap contains a mapping for the specified value. CallsMultiMap.containsKey(K)
on the underlying valuesToKeys MultiMap to see if it contains the given value or not.- Parameters:
value
- The value to check for- Returns:
- Whether this ManyToManyMap contains the given value or not
-
getKeys
Returns the list of keys to which the specified value is mapped, or null if this ManyToManyMap contains no mappings for the value. CallsMultiMap.get(K)
on the underlying valuesToKeys MultiMap.- Parameters:
value
- The value whose mapped keys are being requested- Returns:
- The list of keys mapped to the given value
-
getValues
Returns the list of values to which the specified key is mapped, or null if this ManyToManyMap contains no mappings for the key. CallsMultiMap.get(K)
on the underlying keysToValues MultiMap.- 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 ManyToManyMap. CallsMultiMap.keySet()
on the underlying keysToValues MultiMap.- Returns:
- The Set of keys in this ManyToManyMap
-
valueSet
Returns the Set of values in this ManyToManyMap. CallsMultiMap.keySet()
on the underlying valuesToKeys MultiMap.- Returns:
- The Set of values in this ManyToManyMap
-
keysToValues
Returns the underlying keysToValuesMultiMap
of this ManyToManyMap.- Returns:
- The underlying keysToValues MultiMap
-
valuesToKeys
Returns the underlying valuesToKeysMultiMap
of this ManyToManyMap.- Returns:
- The underlying valuesToKeys MultiMap
-
put
Associates the specified value with the specified key in this ManyToManyMap. Will callMultiMap.put(K, V)
on both underlying MultiMaps.- Parameters:
key
- The key to associate with the given valuevalue
- The value to associate with the given key
-
putAllKeys
Associates all of the given keys with the given value. CallsMultiMap.putAll(Object, List)
on the underlying valuesToKeys MultiMap andMultiMap.put(K, V)
on the underlying keysToValues MultiMap for each of the given keys.- Parameters:
value
- The value to associate with the given keyskeys
- The keys to associate with the given value
-
putAllValues
Associates all of the given values with the given key. CallsMultiMap.putAll(Object, List)
on the underlying keysToValues MultiMap andMultiMap.put(K, V)
on the underlying valuesToKeys MultiMap for each of the given values.- Parameters:
key
- The key to associate with the given valuesvalues
- The values to associate with the given key
-
putAllKeyValMappings
Puts all the given key-value Pairs into this ManyToManyMap.
CallsMultiMap.putAll(Pair[])
on the underlying keysToValuesMultiMap
andMultiMap.put(K, V)
on the underlying valuesToKeys MultiMap for each key-value mapping.- Parameters:
entries
- The key-value Pairs to be put in this ManyToManyMap
-
putAllValKeyMappings
Puts all the given value-key Pairs into this ManyToManyMap.
CallsMultiMap.putAll(Pair[])
on the underlying valuesToKeysMultiMap
andMultiMap.put(K, V)
on the underlying keysToValues MultiMap for each value-key mapping.- Parameters:
entries
- The value-key Pairs to be put in this ManyToManyMap
-
putAllKeyValMappings
Associates all of the key-value mappings from the given Map into this ManyToManyMap.
CallsMultiMap.putAll(Map)
on the underlying keysToValuesMultiMap
andMultiMap.put(K, V)
on the underlying valuesToKeys MultiMap for each key-value mapping.- Parameters:
map
- The map whose mappings should be added to this
-
putAllValKeyMappings
Associates all of the value-key mappings from the given Map into this ManyToManyMap.
CallsMultiMap.putAll(Map)
on the underlying valuesToKeysMultiMap
andMultiMap.put(K, V)
on the underlying keysToValues MultiMap for each key-value mapping.- Parameters:
map
- The map whose mappings should be added to this
-
putAllKeyValMappings
Associates all of the key-value mappings from the given MultiMap into this ManyToManyMap.
CallsMultiMap.putAll(Map)
on the underlying keysToValuesMultiMap
andMultiMap.put(K, V)
on the underlying valuesToKeys MultiMap for each key-value mapping.- Parameters:
map
- The MultiMap whose mappings should be added to this
-
putAllValKeyMappings
Associates all of the value-key mappings from the given MultiMap into this ManyToManyMap.
CallsMultiMap.putAll(Map)
on the underlying valuesToKeysMultiMap
andMultiMap.put(K, V)
on the underlying keysToValues MultiMap for each key-value mapping.- Parameters:
map
- The MultiMap whose mappings should be added to this
-
putAllKeyValMappings
Associates all of the key-value mappings from the given ManyToManyMap into this ManyToManyMap.
Callsput(K, V)
for each of the key-value mappings of the given ManyToManyMap.- Parameters:
map
- The ManyToManyMap whose mappings should be added to this
-
putAllValKeyMappings
Associates all of the value-key mappings from the given ManyToManyMap into this ManyToManyMap.
Callsput(K, V)
for each of the value-key mappings of the given ManyToManyMap.- Parameters:
map
- The ManyToManyMap whose mappings should be added to this
-
removeKey
Removes all values associated with the given key from the ManyToManyMap. CallsMultiMap.removeKey(Object)
on the underlying keysToValues MultiMap and thenMultiMap.remove(Object, Object)
on the underlying valuesToKeys MultiMap for each of the former key-value pairs being removed.- Parameters:
key
- The key whose associations are to be removed- Returns:
- The List of values the key used to be associated with
-
removeValue
Removes all keys associated with the given value from the ManyToManyMap. CallsMultiMap.removeKey(Object)
on the underlying valuesToKeys MultiMap and thenMultiMap.remove(Object, Object)
on the underlying keysToValues MultiMap for each of the former key-value pairs being removed.- Parameters:
value
- The value whose associations are to be removed- Returns:
- The List of keys the value used to be associated with
-
remove
Removes the specified value from being associated with the specified key, if the mapping exists.
Will return whether the mapping existed or not.
CallsMultiMap.remove(Object, Object)
on the underlying keysToValues MultiMap and if that returns true, calls the same method on the underlying valuesToKeys MultiMap to remove it entirely.- 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
-
removeEntireKeysList
Removes the given list of keys associated with the given value if the list of keys matches the current list associated with that value.
CallsMultiMap.removeEntireList(K, java.util.List<V>)
on the underlying valuesToKeysMultiMap
. If that returns true,MultiMap.remove(Object, Object)
is then called on the keysToValues MultiMap for each of the key-value mappings.- Parameters:
value
- The value of the given value-key association to removekeys
- The keys of the given value-key association to remove- Returns:
- Whether the list of keys was removed or not
-
removeEntireValuesList
Removes the given list of values associated with the given key if the list of values matches the current list associated with that key.
CallsMultiMap.removeEntireList(K, java.util.List<V>)
on the underlying keysToValuesMultiMap
. If that returns true,MultiMap.remove(Object, Object)
is then called on the valuesToKeys MultiMap for each of the key-value mappings.- 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
-
replaceEntireKeyList
Replaces the current list of keys associated with the given value with the given list of keys, only if it is currently mapped to a value.
CallsMultiMap.replaceEntireList(Object, List)
on the underlying valuesToKeysMultiMap
and thenremoves
the old keys from the keysToValues MultiMap andputs
the new keys in it.- Parameters:
value
- The value to change the associations ofkeys
- The keys to associate with the given value- Returns:
- The previous list of keys associated with the given value
-
replaceEntireValueList
Replaces the current list of values associated with the given key with the given list of values.
CallsMultiMap.replaceEntireList(Object, List)
on the underlying keysToValuesMultiMap
and thenremoves
the old values from the valuesToKeys MultiMap andputs
the new values in it.- 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
-
replaceKey
Replaces the given old key with the given new key for an association to the given value, if the old key is currently associated with the given value.
CallsMultiMap.replace(K, V, V)
on the underlying valuesToKeysMultiMap
and if that returns true, itremoves
the old key from the keysToValues MultiMap andputs
the new key in it at the end of the list.- Parameters:
value
- The value to change the association ofoldKey
- The old key associated with the given valuenewKey
- The new key to associate with the given value- Returns:
- true if the key was replaced
-
replaceValue
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.
CallsMultiMap.replace(K, V, V)
on the underlying keysToValuesMultiMap
and if that returns true, itremoves
the old value from the valuesToKeys MultiMap andputs
the new value in it at the end of the list.- 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
-
replaceEntireKeyList
Replaces the current list of keys associated with the given value with the given new list of keys if the given old list matches the current list.
CallsMultiMap.replaceEntireList(K, java.util.List<V>, java.util.List<V>)
on the underlying valuesToKeysMultiMap
and if that returns true, it thenremoves
the old key-value mappings from the keysToValues MultiMap andputs
the new key-value mappings in it.- Parameters:
value
- The value to change the associations ofoldKeys
- The old list of keys associated with the given valuenewKeys
- The keys to associate with the given value- Returns:
- true if the keys were replaced
-
replaceEntireValueList
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.
CallsMultiMap.replaceEntireList(K, java.util.List<V>, java.util.List<V>)
on the underlying keysToValuesMultiMap
and if that returns true, it thenremoves
the old value-key mappings from the valuesToKeys MultiMap andputs
the new value-key mappings in it.- 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
-
keySetSize
public final int keySetSize()Returns the number of keys currently in this ManyToManyMap.
CallsMultiMap.keySetSize()
on the underlying keysToValuesMultiMap
.- Returns:
- The number of keys currently in this ManyToManyMap
-
valueSetSize
public final int valueSetSize()Returns the number of values currently in this ManyToManyMap.
CallsMultiMap.keySetSize()
on the underlying valuesToKeysMultiMap
.- Returns:
- The number of values currently in this ManyToManyMap
-
size
public final int size()Returns the number of key-value associations of this ManyToManyMap.
CallsMultiMap.size()
on the underlying keysToValuesMultiMap
.- Returns:
- The number of key-value associations of this ManyToManyMap.
-
forEach
Performs the given action for each key-value association in this ManyToManyMap 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 ManyToManyMap. The ManyToManyMap will be empty after this call returns.
CallsMultiMap.clear()
on the underlyingMultiMap
s.
-