Class Multimap<K,​V>

  • Type Parameters:
    K - The key type.
    V - The type of values.
    All Implemented Interfaces:
    java.util.Map<K,​java.util.Collection<V>>

    public class Multimap<K,​V>
    extends java.lang.Object
    implements java.util.Map<K,​java.util.Collection<V>>
    A Map which allows each to be associated with multiple values.
    Since:
    1.6 implements Map interface, additional utility methods
    • 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>
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected boolean isConcurrent  
      protected boolean isReadOptimized  
      protected java.util.Map<K,​java.util.Collection<V>> storage  
    • Constructor Summary

      Constructors 
      Constructor Description
      Multimap()
      Creates a non-concurrent Multimap.
      Multimap​(boolean concurrent)  
      Multimap​(boolean concurrent, boolean readOnly)  
      Multimap​(boolean concurrent, boolean readOnly, java.util.Map<K,​? extends java.util.Collection<V>> other)  
      Multimap​(Multimap<K,​V> other)
      Copy constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Removes all mappings.
      boolean containsKey​(java.lang.Object key)  
      boolean containsValue​(java.lang.Object value)  
      java.util.Set<java.util.Map.Entry<K,​java.util.Collection<V>>> entrySet()  
      boolean equals​(java.lang.Object obj)  
      java.util.Collection<V> get​(java.lang.Object key)
      Returns all values associated with the key.
      java.util.Collection<V> getMutable​(java.lang.Object key)
      Returns all values associated with the key.
      int hashCode()  
      boolean hasKey​(K key)  
      boolean isEmpty()  
      boolean isThreadSafe()  
      java.util.Set<K> keySet()  
      protected java.util.Collection<V> newCollection()
      Convenience method for creating new values for entries.
      protected java.util.Collection<V> newCollection​(java.util.Collection<? extends V> values)
      Convenience method for creating new values for entries.
      protected java.util.Map<K,​java.util.Collection<V>> newMapDelegate()
      Convenience method for creating a new underlying Multimap.
      protected java.util.Map<K,​java.util.Collection<V>> newMapDelegate​(java.util.Map<? extends K,​? extends java.util.Collection<V>> initial)
      Convenience method for creating a new underlying Multimap.
      java.util.Collection<V> put​(K key, java.util.Collection<V> values)  
      java.util.Collection<V> put​(K key, java.util.Collection<V> values, boolean wrap)
      Associates the key with the given values, overwriting the entry if the key is present.
      boolean put​(K key, V value)
      Adds the given value to the associations for the key.
      void putAll​(java.util.Map<? extends K,​? extends java.util.Collection<V>> m)  
      boolean putAll​(K key, java.util.Collection<V> values)
      Associates all of the specified values for the given key.
      boolean putAll​(K key, java.util.Collection<V> values, boolean wrap)
      Associates all of the specified values for the given key.
      protected boolean putAll​(K key, java.util.Collection<V> values, boolean create, boolean replace, boolean wrap)
      Replaces the entry in this Multimap with the specified values.
      boolean putAllIfPresent​(K key, java.util.Collection<V> values)
      Adds all of the specified values to the collection associated with the key if a mapping for the key exists in this Multimap.
      boolean putIfPresent​(java.lang.Object key, V value)
      Associates the value with the key if the key already exists.
      java.util.Collection<V> remove​(java.lang.Object key)
      Deletes the specified key from this map.
      boolean remove​(java.lang.Object key, java.lang.Object value)
      Removes the specified value associated with the given key.
      boolean removeAll​(java.lang.Object key)
      Removes all values associated with the key, leaving the key itself in place, by calling Collection.clear() if the mapping is present.
      boolean removeAll​(java.lang.Object key, java.util.Collection<V> values)
      Removes all of the specified values associated with the key.
      boolean replaceValues​(K key, java.util.Collection<V> values)
      Replaces the values associated with the key with the values from the specified collection.
      int size()  
      int size​(K key)
      Returns the number of values associated with the key.
      java.util.stream.Stream<V> stream​(java.lang.Object key)
      Streams the values for the given key.
      java.util.stream.Stream<V> streamAll()  
      java.lang.String toString()  
      java.util.Collection<java.util.Collection<V>> values()  
      • Methods inherited from class java.lang.Object

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

        compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, replace, replace, replaceAll
    • Field Detail

      • storage

        protected final java.util.Map<K,​java.util.Collection<V>> storage
      • isConcurrent

        protected final boolean isConcurrent
      • isReadOptimized

        protected final boolean isReadOptimized
    • Constructor Detail

      • Multimap

        public Multimap()
        Creates a non-concurrent Multimap.
      • Multimap

        public Multimap​(boolean concurrent)
        Parameters:
        concurrent - Whether this implementation should be thread-safe.
        Since:
        1.6
      • Multimap

        public Multimap​(boolean concurrent,
                        boolean readOnly)
        Parameters:
        concurrent - Whether this implementation should be thread-safe.
        readOnly - Whether this Multimap (i.e. both the underlying Map itself and collections associated with keys) will be immutable or rarely written to.
        Since:
        1.6
      • Multimap

        public Multimap​(boolean concurrent,
                        boolean readOnly,
                        java.util.Map<K,​? extends java.util.Collection<V>> other)
        Parameters:
        concurrent - Whether this implementation should be thread-safe.
        readOnly - Whether this Multimap (i.e. both the underlying Map itself and collections associated with keys) will be immutable or rarely written to.
        other - The Multimap to copy from.
        Since:
        1.6
      • Multimap

        public Multimap​(Multimap<K,​V> other)
        Copy constructor.
        Parameters:
        other - The Multimap to copy from.
        Throws:
        java.lang.NullPointerException - If the Multimap is null.
    • Method Detail

      • newMapDelegate

        protected final java.util.Map<K,​java.util.Collection<V>> newMapDelegate()
        Convenience method for creating a new underlying Multimap.
        Parameters:
        initial - The elements to add to the multimap.
        Since:
        1.6
        See Also:
        newMapDelegate(Map)
      • newMapDelegate

        protected java.util.Map<K,​java.util.Collection<V>> newMapDelegate​(java.util.Map<? extends K,​? extends java.util.Collection<V>> initial)
        Convenience method for creating a new underlying Multimap.
        Parameters:
        initial - The elements to add to the multimap.
        Returns:
        A suitable Map based on the thread-safety of this Multimap.
      • newCollection

        protected final java.util.Collection<V> newCollection()
        Convenience method for creating new values for entries.
        Returns:
        A suitable collection based on the thread-safety of this Multimap.
        Since:
        1.6
        See Also:
        newCollection(Collection)
      • newCollection

        protected java.util.Collection<V> newCollection​(java.util.Collection<? extends V> values)
        Convenience method for creating new values for entries.
        Parameters:
        values - The items to add to the returned collection.
        Returns:
        A suitable collection based on the thread-safety of this Multimap.
        Since:
        1.6
      • get

        public java.util.Collection<V> get​(java.lang.Object key)
        Returns all values associated with the key. The returned collection is immutable.
        Specified by:
        get in interface java.util.Map<K,​V>
        Parameters:
        key -
        Returns:
        The associated values, or an empty collection if the key was not present.
      • getMutable

        public java.util.Collection<V> getMutable​(java.lang.Object key)
        Returns all values associated with the key. The returned collection is the collection used internally to associate the key with its values.
        Parameters:
        key -
        Returns:
        A collection of the associated values, or null if no mapping was present.
        Since:
        1.6
      • put

        public boolean put​(K key,
                           V value)
        Adds the given value to the associations for the key.
        Parameters:
        key -
        value -
        Returns:
        Whether a mapping existed for the key.
      • putIfPresent

        public boolean putIfPresent​(java.lang.Object key,
                                    V value)
        Associates the value with the key if the key already exists. Note that this operation will succeed even if the key has no values associated with it; only if the key is present.
        Parameters:
        key -
        value -
        Returns:
        Whether the key was present and value was added.
        Since:
        1.6
      • putAllIfPresent

        public boolean putAllIfPresent​(K key,
                                       java.util.Collection<V> values)
        Adds all of the specified values to the collection associated with the key if a mapping for the key exists in this Multimap.
        Parameters:
        key -
        values - The additional values to associate with the key.
        Returns:
        Whether the key was present and the values were added.
        Since:
        1.6
      • remove

        public boolean remove​(java.lang.Object key,
                              java.lang.Object value)
        Removes the specified value associated with the given key.
        Specified by:
        remove in interface java.util.Map<K,​V>
        Parameters:
        key -
        value -
        Returns:
        Whether the key was present and the associated collection of values was changed.
      • removeAll

        public boolean removeAll​(java.lang.Object key)
        Removes all values associated with the key, leaving the key itself in place, by calling Collection.clear() if the mapping is present.
        Parameters:
        key -
        Returns:
        Whether the key was present and the collection changed as a result.
        Since:
        1.6
      • removeAll

        public boolean removeAll​(java.lang.Object key,
                                 java.util.Collection<V> values)
        Removes all of the specified values associated with the key. Note that if the collection contains all values associated with the key, the key itself will not be removed.
        Parameters:
        key -
        values - The values to remove.
        Returns:
        Whether the key was present and the associated collection of values was changed as a result of this call.
        Since:
        1.6
      • remove

        public java.util.Collection<V> remove​(java.lang.Object key)
        Deletes the specified key from this map.
        Specified by:
        remove in interface java.util.Map<K,​V>
        Parameters:
        key -
        Returns:
        The values associated with the key, or null if no mapping was present.
      • clear

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

        public boolean containsKey​(java.lang.Object key)
        Specified by:
        containsKey in interface java.util.Map<K,​V>
        Parameters:
        key -
        Returns:
        Whether the given key has one or more values associated with it.
      • hasKey

        public boolean hasKey​(K key)
        Parameters:
        key -
        Returns:
        Whether the key is present in they keySet.
        Since:
        1.6
      • putAll

        public final boolean putAll​(K key,
                                    java.util.Collection<V> values)
        Associates all of the specified values for the given key. Previously associated values will not be removed.
        Parameters:
        key -
        values - The additional values to associate with the key.
        Returns:
        Whether the key was present.
        Since:
        1.6 Values are wrapped into the appropriate collection, and existing values are appended to rather than replaced.
      • putAll

        public boolean putAll​(K key,
                              java.util.Collection<V> values,
                              boolean wrap)
        Associates all of the specified values for the given key. Previously associated values will not be removed.
        Parameters:
        key -
        values - The additional values to associate with the key.
        wrap - Whether to wrap the provided values into a new collection.
        Returns:
        Whether the key was present.
        Since:
        1.6
      • replaceValues

        public boolean replaceValues​(K key,
                                     java.util.Collection<V> values)
        Replaces the values associated with the key with the values from the specified collection. The Collection associated with the key itself will not be replaced; only its values will be. Note that unlike putAll, a new entry will not be created if a mapping for the key doesn't exist.
        Parameters:
        key -
        values - The collection of values to copy from.
        Returns:
        Whether the collection was changed as a result of
        Since:
        1.6
      • put

        public java.util.Collection<V> put​(K key,
                                           java.util.Collection<V> values,
                                           boolean wrap)
        Associates the key with the given values, overwriting the entry if the key is present. Note that the associated collection will not be the same object as the parameter.
        Parameters:
        key -
        values - The values to copy from.
        wrap - Whether to wrap the provided values into a new collection.
        Returns:
        The previously associated values, or null if no mapping was present.
        Since:
        1.6
      • put

        public java.util.Collection<V> put​(K key,
                                           java.util.Collection<V> values)
        Specified by:
        put in interface java.util.Map<K,​V>
      • putAll

        protected boolean putAll​(K key,
                                 java.util.Collection<V> values,
                                 boolean create,
                                 boolean replace,
                                 boolean wrap)
        Replaces the entry in this Multimap with the specified values.
        Parameters:
        key -
        values - The values to associate with the key.
        create - Creates a new entry with the values if one doesn't exist.
        replace - Whether to append or replace existing values.
        wrap - Whether to wrap the values into a known data structure.
        Returns:
        Whether the operation had an effect on this Multimap.
        Since:
        1.6
      • isThreadSafe

        public boolean isThreadSafe()
        Returns:
        Since:
        1.6
      • stream

        public java.util.stream.Stream<V> stream​(java.lang.Object key)
        Streams the values for the given key.
        Parameters:
        key -
        Returns:
        The stream of values for the key, or an empty stream if the key is not present.
        Since:
        1.6
      • streamAll

        public java.util.stream.Stream<V> streamAll()
        Returns:
        A flattened Stream of all values in this Multimap.
        Since:
        1.6
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​java.util.Collection<V>>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<K,​V>
        Returns:
        An immutable KeySetView of this Multimap.
        Since:
        1.6
      • size

        public int size()
        Specified by:
        size in interface java.util.Map<K,​V>
        Returns:
        The number of keys.
        Since:
        1.6
      • size

        public int size​(K key)
        Returns the number of values associated with the key.
        Parameters:
        key -
        Returns:
        The number of associated values, or -1 if the key was not present.
        Since:
        1.6
      • equals

        public boolean equals​(java.lang.Object obj)
        Specified by:
        equals in interface java.util.Map<K,​V>
        Overrides:
        equals in class java.lang.Object
        Since:
        1.6
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Map<K,​V>
        Overrides:
        hashCode in class java.lang.Object
        Since:
        1.6
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
        Since:
        1.6
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Specified by:
        containsValue in interface java.util.Map<K,​V>
        Since:
        1.6
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends java.util.Collection<V>> m)
        Specified by:
        putAll in interface java.util.Map<K,​V>
        Since:
        1.6
      • keySet

        public java.util.Set<K> keySet()
        Specified by:
        keySet in interface java.util.Map<K,​V>
        Since:
        1.6
      • values

        public java.util.Collection<java.util.Collection<V>> values()
        Specified by:
        values in interface java.util.Map<K,​V>
        Since:
        1.6