Class ArrayListMultimap<K,V>
- java.lang.Object
-
- com.google.common.collect.AbstractMultimap<K,V>
-
- com.google.common.collect.AbstractMapBasedMultimap<K,V>
-
- com.google.common.collect.AbstractListMultimap<K,V>
-
- com.google.common.collect.ArrayListMultimapGwtSerializationDependencies<K,V>
-
- com.google.common.collect.ArrayListMultimap<K,V>
-
- All Implemented Interfaces:
ListMultimap<K,V>
,Multimap<K,V>
,java.io.Serializable
public final class ArrayListMultimap<K,V> extends ArrayListMultimapGwtSerializationDependencies<K,V>
Implementation ofMultimap
that uses anArrayList
to store the values for a given key. AHashMap
associates each key with anArrayList
of values.When iterating through the collections supplied by this class, the ordering of values for a given key agrees with the order in which the values were added.
This multimap allows duplicate key-value pairs. After adding a new key-value pair equal to an existing key-value pair, the
ArrayListMultimap
will contain entries for both the new value and the old value.Keys and values may be null. All optional multimap methods are supported, and all returned views are modifiable.
The lists returned by
AbstractListMultimap.get(K)
,AbstractListMultimap.removeAll(java.lang.Object)
, andAbstractListMultimap.replaceValues(K, java.lang.Iterable<? extends V>)
all implementRandomAccess
.This class is not threadsafe when any concurrent operations update the multimap. Concurrent read operations will work correctly. To allow concurrent update operations, wrap your multimap with a call to
Multimaps.synchronizedListMultimap(com.google.common.collect.ListMultimap<K, V>)
.See the Guava User Guide article on
Multimap
.- Since:
- 2.0
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.google.common.collect.AbstractMapBasedMultimap
AbstractMapBasedMultimap.NavigableAsMap, AbstractMapBasedMultimap.NavigableKeySet, AbstractMapBasedMultimap.WrappedCollection, AbstractMapBasedMultimap.WrappedList, AbstractMapBasedMultimap.WrappedNavigableSet, AbstractMapBasedMultimap.WrappedSet, AbstractMapBasedMultimap.WrappedSortedSet
-
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 static int
DEFAULT_VALUES_PER_KEY
(package private) int
expectedValuesPerKey
private static long
serialVersionUID
-
Constructor Summary
Constructors Modifier Constructor Description private
ArrayListMultimap()
private
ArrayListMultimap(int expectedKeys, int expectedValuesPerKey)
private
ArrayListMultimap(Multimap<? extends K,? extends V> multimap)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <K,V>
ArrayListMultimap<K,V>create()
Creates a new, emptyArrayListMultimap
with the default initial capacities.static <K,V>
ArrayListMultimap<K,V>create(int expectedKeys, int expectedValuesPerKey)
Constructs an emptyArrayListMultimap
with enough capacity to hold the specified numbers of keys and values without resizing.static <K,V>
ArrayListMultimap<K,V>create(Multimap<? extends K,? extends V> multimap)
Constructs anArrayListMultimap
with the same mappings as the specified multimap.(package private) java.util.List<V>
createCollection()
Creates a new, emptyArrayList
to hold the collection of values for an arbitrary key.private void
readObject(java.io.ObjectInputStream stream)
void
trimToSize()
Deprecated.For aListMultimap
that automatically trims to size, useImmutableListMultimap
.private void
writeObject(java.io.ObjectOutputStream stream)
-
Methods inherited from class com.google.common.collect.AbstractListMultimap
asMap, createUnmodifiableEmptyCollection, equals, get, put, removeAll, replaceValues, unmodifiableCollectionSubclass, wrapCollection
-
Methods inherited from class com.google.common.collect.AbstractMapBasedMultimap
backingMap, clear, containsKey, createAsMap, createCollection, createEntries, createKeys, createKeySet, createMaybeNavigableAsMap, createMaybeNavigableKeySet, createValues, entries, entryIterator, entrySpliterator, forEach, setMap, size, valueIterator, values, valueSpliterator, wrapList
-
Methods inherited from class com.google.common.collect.AbstractMultimap
containsEntry, containsValue, hashCode, isEmpty, keys, keySet, putAll, putAll, remove, toString
-
-
-
-
Field Detail
-
DEFAULT_VALUES_PER_KEY
private static final int DEFAULT_VALUES_PER_KEY
- See Also:
- Constant Field Values
-
expectedValuesPerKey
transient int expectedValuesPerKey
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
-
Method Detail
-
create
public static <K,V> ArrayListMultimap<K,V> create()
Creates a new, emptyArrayListMultimap
with the default initial capacities.This method will soon be deprecated in favor of
MultimapBuilder.hashKeys().arrayListValues().build()
.
-
create
public static <K,V> ArrayListMultimap<K,V> create(int expectedKeys, int expectedValuesPerKey)
Constructs an emptyArrayListMultimap
with enough capacity to hold the specified numbers of keys and values without resizing.This method will soon be deprecated in favor of
MultimapBuilder.hashKeys(expectedKeys).arrayListValues(expectedValuesPerKey).build()
.- Parameters:
expectedKeys
- the expected number of distinct keysexpectedValuesPerKey
- the expected average number of values per key- Throws:
java.lang.IllegalArgumentException
- ifexpectedKeys
orexpectedValuesPerKey
is negative
-
create
public static <K,V> ArrayListMultimap<K,V> create(Multimap<? extends K,? extends V> multimap)
Constructs anArrayListMultimap
with the same mappings as the specified multimap.This method will soon be deprecated in favor of
MultimapBuilder.hashKeys().arrayListValues().build(multimap)
.- Parameters:
multimap
- the multimap whose contents are copied to this multimap
-
createCollection
java.util.List<V> createCollection()
Creates a new, emptyArrayList
to hold the collection of values for an arbitrary key.- Specified by:
createCollection
in classAbstractListMultimap<K,V>
- Returns:
- an empty collection of values
-
trimToSize
@Deprecated public void trimToSize()
Deprecated.For aListMultimap
that automatically trims to size, useImmutableListMultimap
. If you need a mutable collection, remove thetrimToSize
call, or switch to aHashMap<K, ArrayList<V>>
.Reduces the memory used by thisArrayListMultimap
, if feasible.
-
writeObject
private void writeObject(java.io.ObjectOutputStream stream) throws java.io.IOException
- Throws:
java.io.IOException
-
readObject
private void readObject(java.io.ObjectInputStream stream) throws java.io.IOException, java.lang.ClassNotFoundException
- Throws:
java.io.IOException
java.lang.ClassNotFoundException
-
-