Class AbstractSetMultimap<K,V>

All Implemented Interfaces:
Multimap<K,V>, SetMultimap<K,V>, Serializable
Direct Known Subclasses:
AbstractSortedSetMultimap, HashMultimapGwtSerializationDependencies, LinkedHashMultimapGwtSerializationDependencies, Multimaps.CustomSetMultimap

abstract class AbstractSetMultimap<K,V> extends AbstractMapBasedMultimap<K,V> implements SetMultimap<K,V>
Basic implementation of the SetMultimap interface. It's a wrapper around AbstractMapBasedMultimap that converts the returned collections into Sets. The createCollection() method must return a Set.
  • Field Details

  • Constructor Details

    • AbstractSetMultimap

      protected AbstractSetMultimap(Map<K,Collection<V>> map)
      Creates a new multimap that uses the provided map.
      Parameters:
      map - place to store the mapping from each key to its corresponding values
  • Method Details

    • createCollection

      abstract Set<V> createCollection()
      Description copied from class: AbstractMapBasedMultimap
      Creates the collection of values for a single key.

      Collections with weak, soft, or phantom references are not supported. Each call to createCollection should create a new instance.

      The returned collection class determines whether duplicate key-value pairs are allowed.

      Specified by:
      createCollection in class AbstractMapBasedMultimap<K,V>
      Returns:
      an empty collection of values
    • createUnmodifiableEmptyCollection

      Set<V> createUnmodifiableEmptyCollection()
      Description copied from class: AbstractMapBasedMultimap
      Creates an unmodifiable, empty collection of values.

      This is used in AbstractMapBasedMultimap.removeAll(java.lang.Object) on an empty key.

      Overrides:
      createUnmodifiableEmptyCollection in class AbstractMapBasedMultimap<K,V>
    • unmodifiableCollectionSubclass

      <E> Collection<E> unmodifiableCollectionSubclass(Collection<E> collection)
      Overrides:
      unmodifiableCollectionSubclass in class AbstractMapBasedMultimap<K,V>
    • wrapCollection

      Collection<V> wrapCollection(K key, Collection<V> collection)
      Description copied from class: AbstractMapBasedMultimap
      Generates a decorated collection that remains consistent with the values in the multimap for the provided key. Changes to the multimap may alter the returned collection, and vice versa.
      Overrides:
      wrapCollection in class AbstractMapBasedMultimap<K,V>
    • get

      public Set<V> get(K key)
      Returns a view collection of the values associated with key in this multimap, if any. Note that when containsKey(key) is false, this returns an empty collection, not null.

      Changes to the returned collection will update the underlying multimap, and vice versa.

      The returned collection is not serializable.

      Because a SetMultimap has unique values for a given key, this method returns a Set, instead of the Collection specified in the Multimap interface.

      Specified by:
      get in interface Multimap<K,V>
      Specified by:
      get in interface SetMultimap<K,V>
      Overrides:
      get in class AbstractMapBasedMultimap<K,V>
    • entries

      public Set<Map.Entry<K,V>> entries()
      Returns a view collection of all key-value pairs contained in this multimap, as Map.Entry instances.

      Changes to the returned collection or the entries it contains will update the underlying multimap, and vice versa. However, adding to the returned collection is not possible.

      The iterator generated by the returned collection traverses the values for one key, followed by the values of a second key, and so on.

      Each entry is an immutable snapshot of a key-value mapping in the multimap, taken at the time the entry is returned by a method call to the collection or its iterator.

      Because a SetMultimap has unique values for a given key, this method returns a Set, instead of the Collection specified in the Multimap interface.

      Specified by:
      entries in interface Multimap<K,V>
      Specified by:
      entries in interface SetMultimap<K,V>
      Overrides:
      entries in class AbstractMapBasedMultimap<K,V>
    • removeAll

      public Set<V> removeAll(@CheckForNull Object 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 Multimap.keySet(), Multimap.asMap(), or any other views.

      The returned collection is immutable.

      Because a SetMultimap has unique values for a given key, this method returns a Set, instead of the Collection specified in the Multimap interface.

      Specified by:
      removeAll in interface Multimap<K,V>
      Specified by:
      removeAll in interface SetMultimap<K,V>
      Overrides:
      removeAll in class AbstractMapBasedMultimap<K,V>
      Returns:
      the values that were removed (possibly empty). The returned collection may be modifiable, but updating it will have no effect on the multimap.
    • replaceValues

      public Set<V> replaceValues(K key, Iterable<? extends V> values)
      Stores a collection of values with the same key, replacing any existing values for that key.

      If values is empty, this is equivalent to removeAll(key).

      The returned collection is immutable.

      Because a SetMultimap has unique values for a given key, this method returns a Set, instead of the Collection specified in the Multimap interface.

      Any duplicates in values will be stored in the multimap once.

      Specified by:
      replaceValues in interface Multimap<K,V>
      Specified by:
      replaceValues in interface SetMultimap<K,V>
      Overrides:
      replaceValues in class AbstractMapBasedMultimap<K,V>
      Returns:
      the collection of replaced values, or an empty collection if no values were previously associated with the key. The collection may be modifiable, but updating it will have no effect on the multimap.
    • asMap

      public Map<K,Collection<V>> asMap()
      Returns a view of this multimap as a Map from each distinct key to the nonempty collection of that key's associated values. Note that this.asMap().get(k) is equivalent to this.get(k) only when k is a key contained in the multimap; otherwise it returns null as opposed to an empty collection.

      Changes to the returned map or the collections that serve as its values will update the underlying multimap, and vice versa. The map does not support put or putAll, nor do its entries support setValue.

      Though the method signature doesn't say so explicitly, the returned map has Set values.

      Specified by:
      asMap in interface Multimap<K,V>
      Specified by:
      asMap in interface SetMultimap<K,V>
      Overrides:
      asMap in class AbstractMultimap<K,V>
    • put

      public boolean put(K key, V value)
      Stores a key-value pair in the multimap.
      Specified by:
      put in interface Multimap<K,V>
      Overrides:
      put in class AbstractMapBasedMultimap<K,V>
      Parameters:
      key - key to store in the multimap
      value - value to store in the multimap
      Returns:
      true if the method increased the size of the multimap, or false if the multimap already contained the key-value pair
    • equals

      public boolean equals(@CheckForNull Object object)
      Compares the specified object to this multimap for equality.

      Two SetMultimap instances are equal if, for each key, they contain the same values. Equality does not depend on the ordering of keys or values.

      Specified by:
      equals in interface Multimap<K,V>
      Specified by:
      equals in interface SetMultimap<K,V>
      Overrides:
      equals in class AbstractMultimap<K,V>