Class HashMultimap<K,V>
- All Implemented Interfaces:
Multimap<K,
,V> SetMultimap<K,
,V> Serializable
Multimap
using hash tables.
The multimap does not store duplicate key-value pairs. Adding a new key-value pair equal to an existing key-value pair has no effect.
Keys and values may be null. All optional multimap methods are supported, and all returned views are modifiable.
This class is not threadsafe when any concurrent operations update the multimap. Concurrent
read operations will work correctly if the last write happens-before any reads. To allow
concurrent update operations, wrap your multimap with a call to Multimaps.synchronizedSetMultimap(com.google.common.collect.SetMultimap<K, V>)
.
Warning: Do not modify either a key or a value of a HashMultimap
in a
way that affects its Object.equals(java.lang.Object)
behavior. Undefined behavior and bugs will result.
- 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
HashMultimap
(int expectedKeys, int expectedValuesPerKey) private
HashMultimap
(Multimap<? extends K, ? extends V> multimap) -
Method Summary
Modifier and TypeMethodDescriptionstatic <K,
V> HashMultimap <K, V> create()
Creates a new, emptyHashMultimap
with the default initial capacities.static <K,
V> HashMultimap <K, V> create
(int expectedKeys, int expectedValuesPerKey) Constructs an emptyHashMultimap
with enough capacity to hold the specified numbers of keys and values without rehashing.static <K,
V> HashMultimap <K, V> Constructs aHashMultimap
with the same mappings as the specified multimap.Creates the collection of values for a single key.private void
readObject
(ObjectInputStream stream) private void
writeObject
(ObjectOutputStream stream) Methods inherited from class com.google.common.collect.AbstractSetMultimap
asMap, createUnmodifiableEmptyCollection, entries, 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, 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
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface com.google.common.collect.Multimap
clear, containsEntry, containsKey, containsValue, forEach, hashCode, isEmpty, keys, keySet, putAll, putAll, remove, size, values
-
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
-
HashMultimap
private HashMultimap() -
HashMultimap
private HashMultimap(int expectedKeys, int expectedValuesPerKey) -
HashMultimap
-
-
Method Details
-
create
Creates a new, emptyHashMultimap
with the default initial capacities.You may also consider the equivalent
MultimapBuilder.hashKeys().hashSetValues().build()
, which provides more control over the underlying data structure. -
create
Constructs an emptyHashMultimap
with enough capacity to hold the specified numbers of keys and values without rehashing.You may also consider the equivalent
MultimapBuilder.hashKeys(expectedKeys).hashSetValues(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 aHashMultimap
with the same mappings as the specified multimap. If a key-value mapping appears multiple times in the input multimap, it only appears once in the constructed multimap.You may also consider the equivalent
MultimapBuilder.hashKeys().hashSetValues().build(multimap)
, which provides more control over the underlying data structure.- Parameters:
multimap
- the multimap whose contents are copied to this multimap
-
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.
Creates an empty
HashSet
for a collection of values for one key.- Specified by:
createCollection
in classAbstractSetMultimap<K,
V> - Returns:
- a new
HashSet
containing a collection of values for one key
-
writeObject
- Throws:
IOException
-
readObject
- Throws:
IOException
ClassNotFoundException
-