Interface MultiMap<K,V>

Type Parameters:
K - type of the keys in this map
V - type of the values in this map
All Known Implementing Classes:
AbstractMultiMap, MapSetMultiMap

public interface MultiMap<K,V>
A collection that maps keys to values, similar to Map, but in which each key may be associated with multiple values. The values associated with the same key contain no duplicates. You can visualize the contents of a multimap either as a map from keys to nonempty collections of values:
  • k1 -> [v1]
  • k2 -> [v2, v3]
  • k3 -> [v2, v5]

... or as a single "flattened" collection of key-value pairs:

  • k1 -> v1
  • k2 -> v2
  • k2 -> v3
  • k3 -> v2
  • k3 -> v5

Note that both null keys and values are not permitted in this map.

  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Removes all key-value pairs from the multimap, leaving it empty.
    boolean
    contains(K key, V value)
     
    boolean
     
    boolean
     
     
    default void
    forEach(BiConsumer<K,V> action)
    Performs the given action for all key-value pairs contained in this multimap.
    void
    Performs the given action for key-(value-set) pairs contained in this multimap.
    get(K key)
     
    boolean
     
     
    boolean
    put(K key, V value)
    Stores a key-value pair in this multimap.
    boolean
    putAll(K key, Collection<? extends V> values)
    Stores a key-value pair in this multimap for each of values, all using the same key, key.=
    boolean
    putAll(MultiMap<? extends K,? extends V> multiMap)
    Stores all key-value pairs of multimap in this multimap.
    boolean
    remove(K key, V value)
    Removes a single key-value pair with the key key and the value value from this multimap, if such exists.
    boolean
    removeAll(K key)
    Removes all values associated with the key key.
    boolean
    removeAll(K key, Collection<? extends V> values)
    Removes all key-value pairs for key and values.
    int
     
     
  • Method Details

    • contains

      boolean contains(K key, V value)
      Returns:
      true if this multimap contains at least one key-value pair with the key key and the value value.
    • containsKey

      boolean containsKey(K key)
      Returns:
      true if this multimap contains at least one key-value pair with the key key.
    • containsValue

      boolean containsValue(V value)
      Returns:
      true if this multimap contains at least one key-value pair with the value value. Note that this operation may be slow compared to containsKey(Object) in some implementations.
    • get

      Set<V> get(K key)
      Returns:
      an unmodifiable view of the values associated with key in this multimap, if key is absent; otherwise, this returns an empty set.
    • put

      boolean put(@Nonnull K key, @Nonnull V value)
      Stores a key-value pair in this multimap.
      Returns:
      true if the multimap changed.
    • putAll

      boolean putAll(@Nonnull K key, @Nonnull Collection<? extends V> values)
      Stores a key-value pair in this multimap for each of values, all using the same key, key.=
    • putAll

      boolean putAll(@Nonnull MultiMap<? extends K,? extends V> multiMap)
      Stores all key-value pairs of multimap in this multimap.
      Returns:
      true if the multimap changed
    • remove

      boolean remove(K key, V value)
      Removes a single key-value pair with the key key and the value value from this multimap, if such exists.
      Returns:
      true if the multimap changed
    • removeAll

      boolean removeAll(K key)
      Removes all values associated with the key key.

      Once this method returns, key will not be mapped to any values, so it will not appear in keySet().

      Returns:
      true if the multimap changed.
    • removeAll

      boolean removeAll(K key, Collection<? extends V> values)
      Removes all key-value pairs for key and values.
      Returns:
      true if the multimap changed.
    • keySet

      Set<K> keySet()
      Returns:
      an unmodifiable view of all distinct keys contained in this multimap. Note that the key set contains a key if and only if this multimap maps that key to at least one value.
    • values

      Collection<V> values()
      Returns:
      an unmodifiable view collection containing the value from each key-value pair contained in this multimap, without collapsing duplicates (so values().size() == size()).
    • entrySet

      Set<Map.Entry<K,V>> entrySet()
      Returns:
      an unmodifiable view of all key-value pairs contained in this multimap, as Map.Entry instances.
    • forEach

      default void forEach(@Nonnull BiConsumer<K,V> action)
      Performs the given action for all key-value pairs contained in this multimap.
    • forEachSet

      void forEachSet(@Nonnull BiConsumer<K,Set<V>> action)
      Performs the given action for key-(value-set) pairs contained in this multimap.
    • clear

      void clear()
      Removes all key-value pairs from the multimap, leaving it empty.
    • isEmpty

      boolean isEmpty()
      Returns:
      true if this multimap is empty.
    • size

      int size()
      Returns:
      the number of key-value pairs in this multimap.