Class LinkedHashMultimap<K,V>
- All Implemented Interfaces:
Multimap<K,
,V> SetMultimap<K,
,V> Serializable
Multimap
that does not allow duplicate key-value entries and that
returns collections whose iterators follow the ordering in which the data was added to the
multimap.
The collections returned by keySet
, keys
, and asMap
iterate through
the keys in the order they were first added to the multimap. Similarly, get
,
removeAll
, and replaceValues
return collections that iterate through the values in the
order they were added. The collections generated by entries
and values
iterate
across the key-value mappings in the order they were added to the multimap.
The iteration ordering of the collections generated by keySet
, keys
, and
asMap
has a few subtleties. As long as the set of keys remains unchanged, adding or
removing mappings does not affect the key iteration order. However, if you remove all values
associated with a key and then add the key back to the multimap, that key will come last in the
key iteration order.
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. 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 LinkedHashMultimap
in a way that affects its Object.equals(java.lang.Object)
behavior. Undefined behavior and bugs will
result.
See the Guava User Guide article on Multimap
.
- Since:
- 2.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final class
LinkedHashMultimap entries are in no less than three coexisting linked lists: a bucket in the hash table for aSet<V>
associated with a key, the linked list of insertion-ordered entries in thatSet<V>
, and the linked list of entries in the LinkedHashMultimap as a whole.(package private) final class
private static interface
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
private static final int
private LinkedHashMultimap.ValueEntry
<K, V> private static final long
(package private) static final double
(package private) int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Removes all key-value pairs from the multimap, leaving it empty.static <K,
V> LinkedHashMultimap <K, V> create()
Creates a new, emptyLinkedHashMultimap
with the default initial capacities.static <K,
V> LinkedHashMultimap <K, V> create
(int expectedKeys, int expectedValuesPerKey) Constructs an emptyLinkedHashMultimap
with enough capacity to hold the specified numbers of keys and values without rehashing.static <K,
V> LinkedHashMultimap <K, V> Constructs aLinkedHashMultimap
with the same mappings as the specified multimap.Creates the collection of values for a single key.(package private) Collection
<V> createCollection
(K key) Creates the collection of values for an explicitly provided key.private static <K,
V> void deleteFromMultimap
(LinkedHashMultimap.ValueEntry<K, V> entry) private static <K,
V> void deleteFromValueSet
(LinkedHashMultimap.ValueSetLink<K, V> entry) entries()
Returns a set of all key-value pairs.Returns an iterator across all key-value map entries, used byentries().iterator()
andvalues().iterator()
.(package private) Spliterator
<Map.Entry<K, V>> keySet()
Returns a view collection of all distinct keys contained in this multimap.private void
readObject
(ObjectInputStream stream) replaceValues
(K key, Iterable<? extends V> values) Stores a collection of values with the same key, replacing any existing values for that key.private static <K,
V> void succeedsInMultimap
(LinkedHashMultimap.ValueEntry<K, V> pred, LinkedHashMultimap.ValueEntry<K, V> succ) private static <K,
V> void succeedsInValueSet
(LinkedHashMultimap.ValueSetLink<K, V> pred, LinkedHashMultimap.ValueSetLink<K, V> succ) values()
Returns a collection of all values in the multimap.(package private) Spliterator
<V> private void
writeObject
(ObjectOutputStream stream) Methods inherited from class com.google.common.collect.AbstractSetMultimap
asMap, createUnmodifiableEmptyCollection, equals, get, put, removeAll, unmodifiableCollectionSubclass, wrapCollection
Methods inherited from class com.google.common.collect.AbstractMapBasedMultimap
backingMap, containsKey, createAsMap, createEntries, createKeys, createKeySet, createMaybeNavigableAsMap, createMaybeNavigableKeySet, createValues, forEach, setMap, size, wrapList
Methods inherited from class com.google.common.collect.AbstractMultimap
containsEntry, containsValue, hashCode, isEmpty, keys, 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
containsEntry, containsKey, containsValue, forEach, hashCode, isEmpty, keys, putAll, putAll, remove, size
-
Field Details
-
DEFAULT_KEY_CAPACITY
private static final int DEFAULT_KEY_CAPACITY- See Also:
-
DEFAULT_VALUE_SET_CAPACITY
private static final int DEFAULT_VALUE_SET_CAPACITY- See Also:
-
VALUE_SET_LOAD_FACTOR
static final double VALUE_SET_LOAD_FACTOR- See Also:
-
valueSetCapacity
transient int valueSetCapacity -
multimapHeaderEntry
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
-
Constructor Details
-
LinkedHashMultimap
private LinkedHashMultimap(int keyCapacity, int valueSetCapacity)
-
-
Method Details
-
create
Creates a new, emptyLinkedHashMultimap
with the default initial capacities. -
create
Constructs an emptyLinkedHashMultimap
with enough capacity to hold the specified numbers of keys and values without rehashing.- Parameters:
expectedKeys
- the expected number of distinct keysexpectedValuesPerKey
- the expected average number of values per key- Throws:
IllegalArgumentException
- ifexpectedKeys
orexpectedValuesPerKey
is negative
-
create
Constructs aLinkedHashMultimap
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. The new multimap has the sameMultimap.entries()
iteration order as the input multimap, except for excluding duplicate mappings.- Parameters:
multimap
- the multimap whose contents are copied to this multimap
-
succeedsInValueSet
private static <K,V> void succeedsInValueSet(LinkedHashMultimap.ValueSetLink<K, V> pred, LinkedHashMultimap.ValueSetLink<K, V> succ) -
succeedsInMultimap
private static <K,V> void succeedsInMultimap(LinkedHashMultimap.ValueEntry<K, V> pred, LinkedHashMultimap.ValueEntry<K, V> succ) -
deleteFromValueSet
-
deleteFromMultimap
-
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
LinkedHashSet
for a collection of values for one key.- Specified by:
createCollection
in classAbstractSetMultimap<K,
V> - Returns:
- a new
LinkedHashSet
containing a collection of values for one key
-
createCollection
Creates the collection of values for an explicitly provided key. By default, it simply callsAbstractMapBasedMultimap.createCollection()
, which is the correct behavior for most implementations. TheLinkedHashMultimap
class overrides it.Creates a decorated insertion-ordered set that also keeps track of the order in which key-value pairs are added to the multimap.
- Overrides:
createCollection
in classAbstractMapBasedMultimap<K,
V> - Parameters:
key
- key to associate with values in the collection- Returns:
- a new decorated set containing a collection of values for one key
-
replaceValues
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.
Because a
SetMultimap
has unique values for a given key, this method returns aSet
, instead of theCollection
specified in theMultimap
interface.Any duplicates in
values
will be stored in the multimap once.If
values
is not empty and the multimap already contains a mapping forkey
, thekeySet()
ordering is unchanged. However, the provided values always come last in theentries()
andvalues()
iteration orderings.- Specified by:
replaceValues
in interfaceMultimap<K,
V> - Specified by:
replaceValues
in interfaceSetMultimap<K,
V> - Overrides:
replaceValues
in classAbstractSetMultimap<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.
-
entries
Returns a set of all key-value pairs. Changes to the returned set will update the underlying multimap, and vice versa. The entries set does not support theadd
oraddAll
operations.The iterator generated by the returned set traverses the entries in the order they were added to the multimap.
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.
-
keySet
Returns a view collection of all distinct keys contained in this multimap. Note that the key set contains a key if and only if this multimap maps that key to at least one value.The iterator generated by the returned set traverses the keys in the order they were first added to the multimap.
Changes to the returned set will update the underlying multimap, and vice versa. However, adding to the returned set is not possible.
-
values
Returns a collection of all values in the multimap. Changes to the returned collection will update the underlying multimap, and vice versa.The iterator generated by the returned collection traverses the values in the order they were added to the multimap.
-
entryIterator
Description copied from class:AbstractMapBasedMultimap
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.- Overrides:
entryIterator
in classAbstractMapBasedMultimap<K,
V> - Returns:
- an iterator across map entries
-
entrySpliterator
Spliterator<Map.Entry<K,V>> entrySpliterator()- Overrides:
entrySpliterator
in classAbstractMapBasedMultimap<K,
V>
-
valueIterator
- Overrides:
valueIterator
in classAbstractMapBasedMultimap<K,
V>
-
valueSpliterator
Spliterator<V> valueSpliterator()- Overrides:
valueSpliterator
in classAbstractMapBasedMultimap<K,
V>
-
clear
public void clear()Description copied from interface:Multimap
Removes all key-value pairs from the multimap, leaving it empty. -
writeObject
- Throws:
IOException
-
readObject
- Throws:
IOException
ClassNotFoundException
-