Class MultiValueMap<K,​V>

  • Type Parameters:
    K - The key type
    V - The value type
    All Implemented Interfaces:
    java.io.Serializable, java.util.Map<K,​java.util.Set<V>>

    public class MultiValueMap<K,​V>
    extends java.lang.Object
    implements java.util.Map<K,​java.util.Set<V>>, java.io.Serializable
    Map implementation that can hold multiple unique values for a given key.
    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Constructor Summary

      Constructors 
      Constructor Description
      MultiValueMap()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.Set<V> addValue​(K key, V o)
      Adds a value for a given key.
      java.util.Set<V> addValues​(K key, java.util.Collection<V> coll)
      Adds values for a given key.
      java.util.Set<V> addValues​(K key, V... o)
      Adds values for a given key.
      void clear()
      Removes all entries from the map.
      boolean containsKey​(java.lang.Object key)
      Determines if the map contains an entry for the given key.
      boolean containsValue​(java.lang.Object value)
      Determines if the map contains a value for any key.
      java.util.Set<java.util.Map.Entry<K,​java.util.Set<V>>> entrySet()
      Gets the Set of Entries.
      java.util.Set<V> get​(java.lang.Object key)
      Returns the first key.
      V getFirstValue​(java.lang.Object key)
      Gets the first/only value for a given key.
      int getValueCount​(java.lang.String key)
      Gets the number of values for a given key.
      java.util.Set<V> getValues​(java.lang.Object key)
      Gets the full Set of values.
      boolean hasData()
      Determines if the map has any entries.
      boolean isEmpty()
      Determines if the map has any data.
      java.util.Set<K> keySet()
      Gets the set of keys.
      java.util.Set<V> put​(K key, java.util.Set<V> value)
      void putAll​(java.util.Map<? extends K,​? extends java.util.Set<V>> map)
      void putOneFromEach​(java.util.Map<K,​V> t)
      Adds a single value for each entry in the map.
      java.util.Set<V> remove​(java.lang.Object key)
      Removes all values for a key.
      java.lang.Object removeValue​(java.lang.String key, java.lang.Object value)
      Removes a specific value for a key.
      void setValues​(K key, java.util.Collection<V> l)
      Sets the values for a given key replacing old ones if they existed.
      int size()
      Gets the size of the map.
      java.lang.String toString()
      Gets a nice string-based representation of the map.
      java.util.Collection<java.util.Set<V>> values()
      Returns a Collection containing each set of values.
      java.util.Collection<V> valuesCollapsed()
      Returns a Collection containing all values from all keys.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Constructor Detail

      • MultiValueMap

        public MultiValueMap()
    • Method Detail

      • getValues

        public java.util.Set<V> getValues​(java.lang.Object key)
        Gets the full Set of values.
        Parameters:
        key - key to lookup values for
        Returns:
        all values for the given key
      • getFirstValue

        public V getFirstValue​(java.lang.Object key)
        Gets the first/only value for a given key.
        Parameters:
        key - key to lookup values for
        Returns:
        the first/only value for a given key, null if not set
      • size

        public int size()
        Gets the size of the map.
        Specified by:
        size in interface java.util.Map<K,​V>
        Returns:
        number of keys in the map
      • setValues

        public void setValues​(K key,
                              java.util.Collection<V> l)
        Sets the values for a given key replacing old ones if they existed.
        Parameters:
        key - key to set values for
        l - the values for the key
      • addValue

        public java.util.Set<V> addValue​(K key,
                                         V o)
        Adds a value for a given key.
        Parameters:
        key - key to add a value to
        o - the objects to add
        Returns:
        the set of objects
      • addValues

        public java.util.Set<V> addValues​(K key,
                                          java.util.Collection<V> coll)
        Adds values for a given key.
        Parameters:
        key - key to add a value to
        coll - the objects to add
        Returns:
        the set of objects
      • addValues

        public java.util.Set<V> addValues​(K key,
                                          V... o)
        Adds values for a given key.
        Parameters:
        key - key to add a value to
        o - the object to add
        Returns:
        the set of objects
      • keySet

        public java.util.Set<K> keySet()
        Gets the set of keys.
        Specified by:
        keySet in interface java.util.Map<K,​V>
        Returns:
        set of keys
      • hasData

        public boolean hasData()
        Determines if the map has any entries.
        Returns:
        true if size()>0
      • toString

        public java.lang.String toString()
        Gets a nice string-based representation of the map.
        Overrides:
        toString in class java.lang.Object
        Returns:
        string-based representation of the map
      • clear

        public void clear()
        Removes all entries from the map.
        Specified by:
        clear in interface java.util.Map<K,​V>
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Determines if the map contains an entry for the given key.
        Specified by:
        containsKey in interface java.util.Map<K,​V>
        Parameters:
        key - key to check
        Returns:
        true if the map contains the key
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Determines if the map contains a value for any key. If value is a Set checks to see if a set matches, otherwise checks for an individual value. Note: uses equals() to check for equality.
        Specified by:
        containsValue in interface java.util.Map<K,​V>
        Parameters:
        value - value in set
        Returns:
        true if the map contains any matching value for any key
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​java.util.Set<V>>> entrySet()
        Gets the Set of Entries.
        Specified by:
        entrySet in interface java.util.Map<K,​V>
        Returns:
        the Set of Entries
      • get

        public java.util.Set<V> get​(java.lang.Object key)
        Returns the first key.
        Specified by:
        get in interface java.util.Map<K,​V>
        Parameters:
        key - the key to lookup
        Returns:
        the first value for the key
        See Also:
        getFirstValue(Object)
      • isEmpty

        public boolean isEmpty()
        Determines if the map has any data.
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
        Returns:
        true of the map has any entries
      • putOneFromEach

        public void putOneFromEach​(java.util.Map<K,​V> t)
        Adds a single value for each entry in the map.
        Parameters:
        t - the map to add
      • remove

        public java.util.Set<V> remove​(java.lang.Object key)
        Removes all values for a key.
        Specified by:
        remove in interface java.util.Map<K,​V>
        Parameters:
        key - the key to remove
        Returns:
        the object that was returned
      • removeValue

        public java.lang.Object removeValue​(java.lang.String key,
                                            java.lang.Object value)
        Removes a specific value for a key.
        Parameters:
        key - the key to lookup
        value - the value to remove
        Returns:
        the removed value
      • values

        public java.util.Collection<java.util.Set<V>> values()
        Returns a Collection containing each set of values. see also valuesCollapsed().
        Specified by:
        values in interface java.util.Map<K,​V>
        Returns:
        the list of all values for all keys
      • valuesCollapsed

        public java.util.Collection<V> valuesCollapsed()
        Returns a Collection containing all values from all keys.
        Returns:
        the list of all values for all keys
      • getValueCount

        public int getValueCount​(java.lang.String key)
        Gets the number of values for a given key.
        Parameters:
        key - the key to lookup
        Returns:
        the number of items
      • put

        public java.util.Set<V> put​(K key,
                                    java.util.Set<V> value)
        Specified by:
        put in interface java.util.Map<K,​V>
        See Also:
        Map.put(java.lang.Object, java.lang.Object)
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends java.util.Set<V>> map)
        Specified by:
        putAll in interface java.util.Map<K,​V>
        See Also:
        Map.putAll(java.util.Map)