Top Description Inners Fields Constructors Methods
com.google.common.collect

pack-priv abstract Class AbstractMapBasedMultimap<K extends @Nullable Object, V extends @Nullable Object>

extends AbstractMultimap<K extends @Nullable Object, V extends @Nullable Object>
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Known Direct Subclasses
com.google.common.collect.AbstractSetMultimap, com.google.common.collect.Multimaps.CustomMultimap, com.google.common.collect.AbstractListMultimap
Annotations
@GwtCompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, .Preconditions.checkState, com.google.common.collect.NullnessCasts.uncheckedCastNullableTToT, java.util.Objects.requireNonNull

Basic implementation of the Multimap interface. This class represents a multimap as a map that associates each key with a collection of values. All methods of Multimap 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 calls createCollection() to create the collection of values for that key. The subclass should not call createCollection() directly, and a new instance should be created every time the method is called.

For example, the subclass could pass a java.util.TreeMap during construction, and createCollection() could return a java.util.TreeSet, 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 a Set, 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 like List 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 to Multimaps#synchronizedMultimap.

For serialization to work, the subclass must specify explicit readObject and writeObject methods.

Authors
Jared Levy, Louis Wasserman

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
private abstract class
private class
private class
AbstractMapBasedMultimap.RandomAccessWrappedList

List decorator that stays in sync with the multimap values for a key and supports rapid random access.

private class
private class
pack-priv class
AbstractMapBasedMultimap.WrappedCollection

Collection decorator that stays in sync with the multimap values for a key.

pack-priv class
AbstractMapBasedMultimap.WrappedList

List decorator that stays in sync with the multimap values for a key.

pack-priv class
pack-priv class
AbstractMapBasedMultimap.WrappedSet

Set decorator that stays in sync with the multimap values for a key.

pack-priv class
AbstractMapBasedMultimap.WrappedSortedSet

SortedSet decorator that stays in sync with the multimap values for a key.

Field Summary

Modifier and TypeField and Description
private transient Map<K, Collection<V>>
private static final long
private transient int

Constructor Summary

AccessConstructor and Description
protected
AbstractMapBasedMultimap(Map<K, Collection<V>>
place to store the mapping from each key to its corresponding values
map
)

Creates a new multimap that uses the provided map.

Method Summary

Modifier and TypeMethod and Description
pack-priv Map<K, Collection<V>>
public void
clear()

Implements com.google.common.collect.Multimap.clear.

Removes all key-value pairs from the multimap, leaving it empty.
public boolean
containsKey(Object key)

Implements com.google.common.collect.Multimap.containsKey.

Returns true if this multimap contains at least one key-value pair with the key key.
pack-priv Map<K, Collection<V>>
pack-priv abstract Collection<V>

Returns:

an empty collection of values
createCollection
()

Creates the collection of values for a single key.

pack-priv Collection<V>

Returns:

an empty collection of values
createCollection
(K
key to associate with values in the collection
key
)

Creates the collection of values for an explicitly provided key.

pack-priv Collection<Map.Entry<K, V>>
pack-priv Multiset<K>
pack-priv Set<K>
pack-priv final Map<K, Collection<V>>
pack-priv final Set<K>
pack-priv Collection<V>
createUnmodifiableEmptyCollection()

Creates an unmodifiable, empty collection of values.

pack-priv Collection<V>
public Collection<Map.Entry<K, V>>
entries()

Overrides com.google.common.collect.AbstractMultimap.entries.

Implements com.google.common.collect.Multimap.entries.

Returns a view collection of all key-value pairs contained in this multimap, as Entry instances.
pack-priv Iterator<Map.Entry<K, V>>

Returns:

an iterator across map entries
entryIterator
()

Implements abstract com.google.common.collect.AbstractMultimap.entryIterator.

Returns an iterator across all key-value map entries, used by entries().iterator() and values().iterator().
pack-priv Spliterator<Map.Entry<K, V>>
public void
forEach(BiConsumer<? super K, ? super V> action)

Overrides default com.google.common.collect.Multimap.forEach.

Performs the given action for all key-value pairs contained in this multimap.
public Collection<V>
get(K key)

Implements com.google.common.collect.Multimap.get.

Returns a view collection of the values associated with key in this multimap, if any.
private Collection<V>
private static <E extends @Nullable Object> Iterator<E>
public boolean
put(K key, V value)

Overrides com.google.common.collect.AbstractMultimap.put.

Implements com.google.common.collect.Multimap.put.

Stores a key-value pair in this multimap.
public Collection<V>
removeAll(Object key)

Implements com.google.common.collect.Multimap.removeAll.

Removes all values associated with the key key.
private void
removeValuesForKey(Object key)

Removes all values for the provided key.

public Collection<V>
replaceValues(K key, Iterable<? extends V> values)

Overrides com.google.common.collect.AbstractMultimap.replaceValues.

Implements com.google.common.collect.Multimap.replaceValues.

Stores a collection of values with the same key, replacing any existing values for that key.
pack-priv final void
setMap(Map<K, Collection<V>> map)

Used during deserialization only.

public int
size()

Implements com.google.common.collect.Multimap.size.

Returns the number of key-value pairs in this multimap.
pack-priv <E extends @Nullable Object> Collection<E>
pack-priv Iterator<V>
public Collection<V>
values()

Overrides com.google.common.collect.AbstractMultimap.values.

Implements com.google.common.collect.Multimap.values.

Returns a view collection containing the value from each key-value pair contained in this multimap, without collapsing duplicates (so values().size() == size()).
pack-priv Spliterator<V>
pack-priv Collection<V>
wrapCollection(K key, Collection<V> collection)

Generates a decorated collection that remains consistent with the values in the multimap for the provided key.

pack-priv final List<V>
wrapList(K key, List<V> list, AbstractMapBasedMultimap<K, V>.WrappedCollection ancestor)

Inherited from com.google.common.collect.AbstractMultimap:
asMapcontainsEntrycontainsValueequalshashCodeisEmptykeyskeySetputAllputAllremovetoString