Class AbstractMapBasedMultimap<K,V>
- java.lang.Object
-
- com.google.common.collect.AbstractMultimap<K,V>
-
- com.google.common.collect.AbstractMapBasedMultimap<K,V>
-
- All Implemented Interfaces:
Multimap<K,V>
,java.io.Serializable
- Direct Known Subclasses:
AbstractListMultimap
,AbstractSetMultimap
,Multimaps.CustomMultimap
abstract class AbstractMapBasedMultimap<K,V> extends AbstractMultimap<K,V> implements java.io.Serializable
Basic implementation of theMultimap
interface. This class represents a multimap as a map that associates each key with a collection of values. All methods ofMultimap
are supported, including those specified as optional in the interface.To implement a multimap, a subclass must define the method
createCollection()
, which creates an empty collection of values for a key.The multimap constructor takes a map that has a single entry for each distinct key. When you insert a key-value pair with a key that isn't already in the multimap,
AbstractMapBasedMultimap
callscreateCollection()
to create the collection of values for that key. The subclass should not callcreateCollection()
directly, and a new instance should be created every time the method is called.For example, the subclass could pass a
TreeMap
during construction, andcreateCollection()
could return aTreeSet
, in which case the multimap's iterators would propagate through the keys and values in sorted order.Keys and values may be null, as long as the underlying collection classes support null elements.
The collections created by
createCollection()
may or may not allow duplicates. If the collection, such as aSet
, does not support duplicates, an added key-value pair will replace an existing pair with the same key and value, if such a pair is present. With collections likeList
that allow duplicates, the collection will keep the existing key-value pairs while adding a new pair.This class is not threadsafe when any concurrent operations update the multimap, even if the underlying map and
createCollection()
method return threadsafe classes. Concurrent read operations will work correctly. To allow concurrent update operations, wrap your multimap with a call toMultimaps.synchronizedMultimap(com.google.common.collect.Multimap<K, V>)
.For serialization to work, the subclass must specify explicit
readObject
andwriteObject
methods.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
AbstractMapBasedMultimap.AsMap
private class
AbstractMapBasedMultimap.Itr<T>
private class
AbstractMapBasedMultimap.KeySet
(package private) class
AbstractMapBasedMultimap.NavigableAsMap
(package private) class
AbstractMapBasedMultimap.NavigableKeySet
private class
AbstractMapBasedMultimap.RandomAccessWrappedList
List decorator that stays in sync with the multimap values for a key and supports rapid random access.private class
AbstractMapBasedMultimap.SortedAsMap
private class
AbstractMapBasedMultimap.SortedKeySet
(package private) class
AbstractMapBasedMultimap.WrappedCollection
Collection decorator that stays in sync with the multimap values for a key.(package private) class
AbstractMapBasedMultimap.WrappedList
List decorator that stays in sync with the multimap values for a key.(package private) class
AbstractMapBasedMultimap.WrappedNavigableSet
(package private) class
AbstractMapBasedMultimap.WrappedSet
Set decorator that stays in sync with the multimap values for a key.(package private) class
AbstractMapBasedMultimap.WrappedSortedSet
SortedSet decorator that stays in sync with the multimap values for a key.-
Nested classes/interfaces inherited from class com.google.common.collect.AbstractMultimap
AbstractMultimap.Entries, AbstractMultimap.EntrySet, AbstractMultimap.Values
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<K,java.util.Collection<V>>
map
private static long
serialVersionUID
private int
totalSize
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractMapBasedMultimap(java.util.Map<K,java.util.Collection<V>> map)
Creates a new multimap that uses the provided map.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description (package private) java.util.Map<K,java.util.Collection<V>>
backingMap()
void
clear()
Removes all key-value pairs from the multimap, leaving it empty.boolean
containsKey(java.lang.Object key)
Returnstrue
if this multimap contains at least one key-value pair with the keykey
.(package private) java.util.Map<K,java.util.Collection<V>>
createAsMap()
(package private) abstract java.util.Collection<V>
createCollection()
Creates the collection of values for a single key.(package private) java.util.Collection<V>
createCollection(K key)
Creates the collection of values for an explicitly provided key.(package private) java.util.Collection<java.util.Map.Entry<K,V>>
createEntries()
(package private) Multiset<K>
createKeys()
(package private) java.util.Set<K>
createKeySet()
(package private) java.util.Map<K,java.util.Collection<V>>
createMaybeNavigableAsMap()
(package private) java.util.Set<K>
createMaybeNavigableKeySet()
(package private) java.util.Collection<V>
createUnmodifiableEmptyCollection()
Creates an unmodifiable, empty collection of values.(package private) java.util.Collection<V>
createValues()
java.util.Collection<java.util.Map.Entry<K,V>>
entries()
Returns a view collection of all key-value pairs contained in this multimap, asMap.Entry
instances.(package private) java.util.Iterator<java.util.Map.Entry<K,V>>
entryIterator()
Returns an iterator across all key-value map entries, used byentries().iterator()
andvalues().iterator()
.(package private) java.util.Spliterator<java.util.Map.Entry<K,V>>
entrySpliterator()
void
forEach(java.util.function.BiConsumer<? super K,? super V> action)
Performs the given action for all key-value pairs contained in this multimap.java.util.Collection<V>
get(K key)
Returns a view collection of the values associated withkey
in this multimap, if any.private java.util.Collection<V>
getOrCreateCollection(K key)
private static <E> java.util.Iterator<E>
iteratorOrListIterator(java.util.Collection<E> collection)
boolean
put(K key, V value)
Stores a key-value pair in this multimap.java.util.Collection<V>
removeAll(java.lang.Object key)
Removes all values associated with the keykey
.private void
removeValuesForKey(java.lang.Object key)
Removes all values for the provided key.java.util.Collection<V>
replaceValues(K key, java.lang.Iterable<? extends V> values)
Stores a collection of values with the same key, replacing any existing values for that key.(package private) void
setMap(java.util.Map<K,java.util.Collection<V>> map)
Used during deserialization only.int
size()
Returns the number of key-value pairs in this multimap.(package private) <E> java.util.Collection<E>
unmodifiableCollectionSubclass(java.util.Collection<E> collection)
(package private) java.util.Iterator<V>
valueIterator()
java.util.Collection<V>
values()
Returns a view collection containing the value from each key-value pair contained in this multimap, without collapsing duplicates (sovalues().size() == size()
).(package private) java.util.Spliterator<V>
valueSpliterator()
(package private) java.util.Collection<V>
wrapCollection(K key, java.util.Collection<V> collection)
Generates a decorated collection that remains consistent with the values in the multimap for the provided key.(package private) java.util.List<V>
wrapList(K key, java.util.List<V> list, AbstractMapBasedMultimap.WrappedCollection ancestor)
-
Methods inherited from class com.google.common.collect.AbstractMultimap
asMap, containsEntry, containsValue, equals, hashCode, isEmpty, keys, keySet, putAll, putAll, remove, toString
-
-
-
-
Field Detail
-
totalSize
private transient int totalSize
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
AbstractMapBasedMultimap
protected AbstractMapBasedMultimap(java.util.Map<K,java.util.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- Throws:
java.lang.IllegalArgumentException
- ifmap
is not empty
-
-
Method Detail
-
setMap
final void setMap(java.util.Map<K,java.util.Collection<V>> map)
Used during deserialization only.
-
createUnmodifiableEmptyCollection
java.util.Collection<V> createUnmodifiableEmptyCollection()
Creates an unmodifiable, empty collection of values.This is used in
removeAll(java.lang.Object)
on an empty key.
-
createCollection
abstract java.util.Collection<V> createCollection()
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.
- Returns:
- an empty collection of values
-
createCollection
java.util.Collection<V> createCollection(K key)
Creates the collection of values for an explicitly provided key. By default, it simply callscreateCollection()
, which is the correct behavior for most implementations. TheLinkedHashMultimap
class overrides it.- Parameters:
key
- key to associate with values in the collection- Returns:
- an empty collection of values
-
size
public int size()
Description copied from interface:Multimap
Returns the number of key-value pairs in this multimap.Note: this method does not return the number of distinct keys in the multimap, which is given by
keySet().size()
orasMap().size()
. See the opening section of theMultimap
class documentation for clarification.
-
containsKey
public boolean containsKey(@CheckForNull java.lang.Object key)
Description copied from interface:Multimap
Returnstrue
if this multimap contains at least one key-value pair with the keykey
.- Specified by:
containsKey
in interfaceMultimap<K,V>
-
put
public boolean put(K key, V value)
Description copied from interface:Multimap
Stores a key-value pair in this multimap.Some multimap implementations allow duplicate key-value pairs, in which case
put
always adds a new key-value pair and increases the multimap size by 1. Other implementations prohibit duplicates, and storing a key-value pair that's already in the multimap has no effect.
-
replaceValues
public java.util.Collection<V> replaceValues(K key, java.lang.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 toremoveAll(key)
.The returned collection is immutable.
- Specified by:
replaceValues
in interfaceMultimap<K,V>
- Overrides:
replaceValues
in classAbstractMultimap<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.
-
removeAll
public java.util.Collection<V> removeAll(@CheckForNull java.lang.Object key)
Removes all values associated with the keykey
.Once this method returns,
key
will not be mapped to any values, so it will not appear inMultimap.keySet()
,Multimap.asMap()
, or any other views.The returned collection is immutable.
-
unmodifiableCollectionSubclass
<E> java.util.Collection<E> unmodifiableCollectionSubclass(java.util.Collection<E> collection)
-
clear
public void clear()
Description copied from interface:Multimap
Removes all key-value pairs from the multimap, leaving it empty.
-
get
public java.util.Collection<V> get(K key)
Returns a view collection of the values associated withkey
in 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.
The returned collection is not serializable.
-
wrapCollection
java.util.Collection<V> wrapCollection(K key, java.util.Collection<V> collection)
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.
-
wrapList
final java.util.List<V> wrapList(K key, java.util.List<V> list, @CheckForNull AbstractMapBasedMultimap.WrappedCollection ancestor)
-
iteratorOrListIterator
private static <E> java.util.Iterator<E> iteratorOrListIterator(java.util.Collection<E> collection)
-
createKeySet
java.util.Set<K> createKeySet()
- Specified by:
createKeySet
in classAbstractMultimap<K,V>
-
createMaybeNavigableKeySet
final java.util.Set<K> createMaybeNavigableKeySet()
-
removeValuesForKey
private void removeValuesForKey(@CheckForNull java.lang.Object key)
Removes all values for the provided key.
-
values
public java.util.Collection<V> values()
Returns a view collection containing the value from each key-value pair contained in this multimap, without collapsing duplicates (sovalues().size() == size()
).Changes to the returned collection 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.
-
createValues
java.util.Collection<V> createValues()
- Specified by:
createValues
in classAbstractMultimap<K,V>
-
valueIterator
java.util.Iterator<V> valueIterator()
- Overrides:
valueIterator
in classAbstractMultimap<K,V>
-
valueSpliterator
java.util.Spliterator<V> valueSpliterator()
- Overrides:
valueSpliterator
in classAbstractMultimap<K,V>
-
createKeys
Multiset<K> createKeys()
- Specified by:
createKeys
in classAbstractMultimap<K,V>
-
entries
public java.util.Collection<java.util.Map.Entry<K,V>> entries()
Returns a view collection of all key-value pairs contained in this multimap, asMap.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.
-
createEntries
java.util.Collection<java.util.Map.Entry<K,V>> createEntries()
- Specified by:
createEntries
in classAbstractMultimap<K,V>
-
entryIterator
java.util.Iterator<java.util.Map.Entry<K,V>> entryIterator()
Returns an iterator across all key-value map entries, used byentries().iterator()
andvalues().iterator()
. The default behavior, which traverses the values for one key, the values for a second key, and so on, suffices for mostAbstractMapBasedMultimap
implementations.- Specified by:
entryIterator
in classAbstractMultimap<K,V>
- Returns:
- an iterator across map entries
-
entrySpliterator
java.util.Spliterator<java.util.Map.Entry<K,V>> entrySpliterator()
- Overrides:
entrySpliterator
in classAbstractMultimap<K,V>
-
forEach
public void forEach(java.util.function.BiConsumer<? super K,? super V> action)
Description copied from interface:Multimap
Performs the given action for all key-value pairs contained in this multimap. If an ordering is specified by theMultimap
implementation, actions will be performed in the order of iteration ofMultimap.entries()
. Exceptions thrown by the action are relayed to the caller.To loop over all keys and their associated value collections, write
Multimaps.asMap(multimap).forEach((key, valueCollection) -> action())
.
-
createAsMap
java.util.Map<K,java.util.Collection<V>> createAsMap()
- Specified by:
createAsMap
in classAbstractMultimap<K,V>
-
-