Class MultiMap<K,V>

java.lang.Object
com.github.tadukoo.util.map.MultiMap<K,V>
Type Parameters:
K - The type of keys in this MultiMap
V - The type of values in this MultiMap
Direct Known Subclasses:
HashMultiMap, TreeMultiMap

public abstract class MultiMap<K,V> extends Object
This class relates keys to values, but allows for one key to reference multiple values.
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

    Fields
    Modifier and Type
    Field
    Description
    private final Map<K,List<V>>
    Underlying Map used to store the key-value pairs
  • Constructor Summary

    Constructors
    Constructor
    Description
    MultiMap(Map<K,List<V>> theMap)
    Sets the backing Map for this MultiMap.
    MultiMap(Map<K,List<V>> theMap, MultiMap<K,V> multiMap)
    Sets the backing Map for this MultiMap and calls putAll for the given multiMap's entries.
    MultiMap(Map<K,List<V>> theMap, Pair<K,V>... entries)
    Sets the backing Map for this MultiMap and calls putAll(Pair[]) for the given Pair entries.
    MultiMap(Map<K,List<V>> theMap, Map<K,V> otherMap)
    Sets the backing Map for this MultiMap and calls putAll for the given otherMap's entries.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the underlying Map of this MultiMap.
    final void
    Removes all of the key-value associations from this MultiMap.
    final boolean
    Returns true if this MultiMap contains a mapping for the specified key.
    final boolean
    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.
    final List<V>
    get(K key)
    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
    Returns true if this map contains no key-value mappings.
    final Set<K>
    Returns the Set of keys in this MultiMap.
    final int
    Returns the number of keys currently in this MultiMap.
    final void
    put(K key, V value)
    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
    putAll(Pair<K,V>... entries)
    Puts all the given Pairs into this MultiMap.
    final void
    putAll(Map<K,V> map)
    Associates all of the key-value mappings from the given Map into this MultiMap.
    final void
    putAll(K key, List<V> values)
    Associates all of the given values with the given key.
    final boolean
    remove(K key, V value)
    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.
    final List<V>
    removeKey(K key)
    Removes all values associated with the given key from the MultiMap.
    final boolean
    replace(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 List<V>
    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
    replaceEntireList(K key, List<V> oldValues, List<V> newValues)
    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
    Returns the number of key-value associations of this MultiMap.
    final List<V>
    Returns the list of values in this MultiMap as a single List.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • theMap

      private final Map<K,List<V>> theMap
      Underlying Map used to store the key-value pairs
  • Constructor Details

    • MultiMap

      public MultiMap(Map<K,List<V>> theMap)
      Sets the backing Map for this MultiMap.
      Parameters:
      theMap - The Map to use for this MultiMap
    • MultiMap

      @SafeVarargs public MultiMap(Map<K,List<V>> theMap, Pair<K,V>... entries)
      Sets the backing Map for this MultiMap and calls putAll(Pair[]) for the given Pair entries.
      Parameters:
      theMap - The Map to use for this MultiMap
      entries - A collection of Pairs to be put in this MultiMap
    • MultiMap

      public MultiMap(Map<K,List<V>> theMap, Map<K,V> otherMap)
      Sets the backing Map for this MultiMap and calls putAll for the given otherMap's entries.
      Parameters:
      theMap - The Map to use for this MultiMap
      otherMap - A Map containing entries to add to this MultiMap
    • MultiMap

      public MultiMap(Map<K,List<V>> theMap, MultiMap<K,V> multiMap)
      Sets the backing Map for this MultiMap and calls putAll for the given multiMap's entries.
      Parameters:
      theMap - The Map to use for this MultiMap
      multiMap - 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. Calls Map.isEmpty() on the underlying Map.
      Returns:
      true if this map contains no key-value mappings.
    • equals

      public boolean equals(Object o)
      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 the asMap() method on both MultiMaps and call Map.equals(java.lang.Object) to compare their mappings.
      Overrides:
      equals in class Object
      Parameters:
      o - The object to be compared for equality with this MultiMap
      Returns:
      true if the given object is equivalent to this MultiMap
    • containsKey

      public final boolean containsKey(K key)
      Returns true if this MultiMap contains a mapping for the specified key. Calls Map.containsKey(java.lang.Object) on the underlying Map 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

      public final boolean containsValue(V value)
      Returns true if this MultiMap maps one or more keys to the specified value. Calls values() 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

      public final List<V> get(K key)
      Returns the list of values to which the specified key is mapped, or null if this MultiMap contains no mappings for the key. Calls Map.get(java.lang.Object) on the underlying Map.
      Parameters:
      key - The key whose mapped values are being requested
      Returns:
      The list of values mapped to the given key
    • keySet

      public final Set<K> keySet()
      Returns the Set of keys in this MultiMap. Calls Map.keySet() on the underlying Map.
      Returns:
      The Set of keys in this MultiMap
    • values

      public final List<V> values()
      Returns the list of values in this MultiMap as a single List. Calls Map.values() on the underlying Map and streams the Collection of Lists to a single List.
      Returns:
      A list of all the values in this MultiMap
    • asMap

      public Map<K,List<V>> asMap()
      Returns the underlying Map of this MultiMap.
      Returns:
      The underlying Map
    • put

      public final void put(K key, V value)
      Associates the specified value with the specified key in this MultiMap. Will create a new ArrayList 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 value
      value - The value to associate with the given key
    • putAll

      public final void putAll(K key, List<V> values)
      Associates all of the given values with the given key.
      Parameters:
      key - The key to associate with the given values
      values - The values to associate with the given key
    • putAll

      @SafeVarargs public final void putAll(Pair<K,V>... entries)
      Puts all the given Pairs into this MultiMap.
      Parameters:
      entries - The entries to put in this MultiMap
    • putAll

      public final void putAll(Map<K,V> map)
      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

      public final void putAll(MultiMap<K,V> map)
      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

      public final boolean remove(K key, V value)
      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 underlying Map.
      Parameters:
      key - The key of the given key-value association to remove
      value - 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

      public final List<V> removeKey(K key)
      Removes all values associated with the given key from the MultiMap. Calls Map.remove(Object) on the underlying Map.
      Parameters:
      key - The key whose associations are to be removed
      Returns:
      The List of values the key used to be associated with
    • removeEntireList

      public 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.
      This method is calling Map.remove(Object,Object) on the underlying Map.
      Parameters:
      key - The key of the given key-value association to remove
      values - The values of the given key-value association to remove
      Returns:
      Whether the list of values was removed or not
    • replace

      public final boolean replace(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.
      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 of
      oldValue - The old value associated with the given key
      newValue - The new value to associate with the given key
      Returns:
      true if the value was replaced
    • replaceEntireList

      public final boolean replaceEntireList(K key, List<V> oldValues, List<V> newValues)
      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.
      Calls Map.replace(Object, Object, Object) on the underlying Map.
      Parameters:
      key - The key to change the associations of
      oldValues - The old list of values associated with the given key
      newValues - The values to associate with the given key
      Returns:
      true if the values were replaced
    • replaceEntireList

      public final List<V> 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.
      Calls Map.replace(Object, Object) on the underlying Map.
      Parameters:
      key - The key to change the associations of
      values - 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.
      Calls Map.size() on the underlying Map.
      Returns:
      The number of keys currently in this MultiMap
    • size

      public final int size()
      Returns the number of key-value associations of this MultiMap.
      Calls values() to get the full list of values and returns the size of it.
      Returns:
      The number of key-value associations of this MultiMap.
    • forEach

      public 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.
      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.
      Calls Map.clear() on the underlying Map.