- All Superinterfaces:
Multimap<K,V>
- All Known Subinterfaces:
SortedSetMultimap<K,V>
- All Known Implementing Classes:
ForwardingSetMultimap,ForwardingSortedSetMultimap,HashMultimap,ImmutableSetMultimap,LinkedHashMultimap,TreeMultimap
Multimap that cannot hold duplicate key-value pairs. Adding a key-value pair that's
already in the multimap has no effect. See the Multimap documentation for information
common to all multimaps.
The get(K), removeAll(java.lang.Object), and replaceValues(K, java.lang.Iterable<? extends V>) methods each return a Set of values, while entries() returns a Set of map entries. Though the method
signature doesn't say so explicitly, the map returned by asMap() has Set values.
If the values corresponding to a single key should be ordered according to a Comparator (or the natural order), see the SortedSetMultimap subinterface.
Since the value collections are sets, the behavior of a SetMultimap is not specified
if key or value objects already present in the multimap change in a manner that affects
equals comparisons. Use caution if mutable objects are used as keys or values in a
SetMultimap.
Warning: Do not modify either a key or a value of a SetMultimap in a way
that affects its Object.equals(java.lang.Object) behavior. Undefined behavior and bugs will result.
See the Guava User Guide article on Multimap.
- Since:
- 2.0
- Author:
- Jared Levy
-
Method Summary
Modifier and TypeMethodDescriptionMap<K, Collection<V>> asMap()Returns a view of this multimap as aMapfrom each distinct key to the nonempty collection of that key's associated values.entries()Returns a view collection of all key-value pairs contained in this multimap, asMap.Entryinstances.booleanCompares the specified object to this multimap for equality.Returns a view collection of the values associated withkeyin this multimap, if any.Removes all values associated with the keykey.replaceValues(K key, Iterable<? extends V> values) Stores a collection of values with the same key, replacing any existing values for that key.
-
Method Details
-
get
Returns a view collection of the values associated withkeyin this multimap, if any. Note that whencontainsKey(key)is false, this returns an empty collection, notnull.Changes to the returned collection will update the underlying multimap, and vice versa.
Because a
SetMultimaphas unique values for a given key, this method returns aSet, instead of theCollectionspecified in theMultimapinterface. -
removeAll
Removes all values associated with the keykey.Once this method returns,
keywill not be mapped to any values, so it will not appear inMultimap.keySet(),Multimap.asMap(), or any other views.Because a
SetMultimaphas unique values for a given key, this method returns aSet, instead of theCollectionspecified in theMultimapinterface. -
replaceValues
Stores a collection of values with the same key, replacing any existing values for that key.If
valuesis empty, this is equivalent toremoveAll(key).Because a
SetMultimaphas unique values for a given key, this method returns aSet, instead of theCollectionspecified in theMultimapinterface.Any duplicates in
valueswill be stored in the multimap once.- Specified by:
replaceValuesin interfaceMultimap<K extends @Nullable Object, V extends @Nullable Object>- 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.
-
entries
Returns a view collection of all key-value pairs contained in this multimap, asMap.Entryinstances.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.
Because a
SetMultimaphas unique values for a given key, this method returns aSet, instead of theCollectionspecified in theMultimapinterface. -
asMap
Map<K, Collection<V>> asMap()Returns a view of this multimap as aMapfrom each distinct key to the nonempty collection of that key's associated values. Note thatthis.asMap().get(k)is equivalent tothis.get(k)only whenkis a key contained in the multimap; otherwise it returnsnullas 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
putorputAll, nor do its entries supportsetValue.Note: The returned map's values are guaranteed to be of type
Set. To obtain this map with the more specific generic typeMap<K, Set<V>>, callMultimaps.asMap(SetMultimap)instead. -
equals
Compares the specified object to this multimap for equality.Two
SetMultimapinstances are equal if, for each key, they contain the same values. Equality does not depend on the ordering of keys or values.An empty
SetMultimapis equal to any other emptyMultimap, including an emptyListMultimap.
-