Class ArrayListMultimap<K,V>
- All Implemented Interfaces:
ListMultimap<K,
,V> Multimap<K,
,V> Serializable
Multimap
that uses an ArrayList
to store the values for a given
key. A HashMap
associates each key with an ArrayList
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)
, and AbstractListMultimap.replaceValues(K, java.lang.Iterable<? extends V>)
all
implement RandomAccess
.
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:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.google.common.collect.AbstractMapBasedMultimap
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
FieldsModifier and TypeFieldDescriptionprivate static final int
(package private) int
private static final long
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
private
ArrayListMultimap
(int expectedKeys, int expectedValuesPerKey) private
ArrayListMultimap
(Multimap<? extends K, ? extends V> multimap) -
Method Summary
Modifier and TypeMethodDescriptionstatic <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> Constructs anArrayListMultimap
with the same mappings as the specified multimap.Creates a new, emptyArrayList
to hold the collection of values for an arbitrary key.private void
readObject
(ObjectInputStream stream) void
Deprecated.private void
writeObject
(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 Details
-
DEFAULT_VALUES_PER_KEY
private static final int DEFAULT_VALUES_PER_KEY- See Also:
-
expectedValuesPerKey
transient int expectedValuesPerKey -
serialVersionUID
private static final long serialVersionUID- See Also:
-
-
Constructor Details
-
ArrayListMultimap
private ArrayListMultimap() -
ArrayListMultimap
private ArrayListMultimap(int expectedKeys, int expectedValuesPerKey) -
ArrayListMultimap
-
-
Method Details
-
create
Creates a new, emptyArrayListMultimap
with the default initial capacities.You may also consider the equivalent
MultimapBuilder.hashKeys().arrayListValues().build()
, which provides more control over the underlying data structure. -
create
Constructs an emptyArrayListMultimap
with enough capacity to hold the specified numbers of keys and values without resizing.You may also consider the equivalent
MultimapBuilder.hashKeys(expectedKeys).arrayListValues(expectedValuesPerKey).build()
, which provides more control over the underlying data structure.- Parameters:
expectedKeys
- the expected number of distinct keysexpectedValuesPerKey
- the expected average number of values per key- Throws:
IllegalArgumentException
- ifexpectedKeys
orexpectedValuesPerKey
is negative
-
create
Constructs anArrayListMultimap
with the same mappings as the specified multimap.You may also consider the equivalent
MultimapBuilder.hashKeys().arrayListValues().build(multimap)
, which provides more control over the underlying data structure.- Parameters:
multimap
- the multimap whose contents are copied to this multimap
-
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.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
- Throws:
IOException
-
readObject
- Throws:
IOException
ClassNotFoundException
-
ListMultimap
that automatically trims to size, useImmutableListMultimap
.