Class LinkedHashMultimap<K,V>
- java.lang.Object
-
- com.google.common.collect.AbstractMultimap<K,V>
-
- com.google.common.collect.AbstractMapBasedMultimap<K,V>
-
- com.google.common.collect.AbstractSetMultimap<K,V>
-
- com.google.common.collect.LinkedHashMultimap<K,V>
-
- All Implemented Interfaces:
Multimap<K,V>
,SetMultimap<K,V>
,java.io.Serializable
@GwtCompatible(serializable=true, emulated=true) public final class LinkedHashMultimap<K,V> extends AbstractSetMultimap<K,V>
Implementation ofMultimap
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
, andasMap
iterate through the keys in the order they were first added to the multimap. Similarly,get
,removeAll
, andreplaceValues
return collections that iterate through the values in the order they were added. The collections generated byentries
andvalues
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
, andasMap
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>)
.See the Guava User Guide article on
Multimap
.- Since:
- 2.0
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
LinkedHashMultimap.ValueEntry<K,V>
LinkedHashMultimap entries are in no less than three coexisting linked lists: a bucket in the hash table for a Setassociated with a key, the linked list of insertion-ordered entries in that Set , and the linked list of entries in the LinkedHashMultimap as a whole. (package private) class
LinkedHashMultimap.ValueSet
private static interface
LinkedHashMultimap.ValueSetLink<K,V>
-
Nested classes/interfaces inherited from class com.google.common.collect.AbstractMapBasedMultimap
AbstractMapBasedMultimap.NavigableAsMap, AbstractMapBasedMultimap.NavigableKeySet, AbstractMapBasedMultimap.WrappedNavigableSet
-
Nested classes/interfaces inherited from class com.google.common.collect.AbstractMultimap
AbstractMultimap.Values
-
-
Field Summary
Fields Modifier and Type Field Description private static int
DEFAULT_KEY_CAPACITY
private static int
DEFAULT_VALUE_SET_CAPACITY
private LinkedHashMultimap.ValueEntry<K,V>
multimapHeaderEntry
private static long
serialVersionUID
(package private) static double
VALUE_SET_LOAD_FACTOR
(package private) int
valueSetCapacity
-
Constructor Summary
Constructors Modifier Constructor Description private
LinkedHashMultimap(int keyCapacity, int valueSetCapacity)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
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>create(Multimap<? extends K,? extends V> multimap)
Constructs aLinkedHashMultimap
with the same mappings as the specified multimap.(package private) java.util.Set<V>
createCollection()
Creates the collection of values for a single key.(package private) java.util.Collection<V>
createCollection(K key)
Creates the collection of values for an explicitly provided key.private static <K,V>
voiddeleteFromMultimap(LinkedHashMultimap.ValueEntry<K,V> entry)
private static <K,V>
voiddeleteFromValueSet(LinkedHashMultimap.ValueSetLink<K,V> entry)
java.util.Set<java.util.Map.Entry<K,V>>
entries()
Returns a set of all key-value pairs.(package private) java.util.Iterator<java.util.Map.Entry<K,V>>
entryIterator()
Returns an iterator across all key-value map entries, used byentries().iterator()
andvalues().iterator()
.java.util.Set<K>
keySet()
Returns a view collection of all distinct keys contained in this multimap.private void
readObject(java.io.ObjectInputStream stream)
java.util.Set<V>
replaceValues(K key, java.lang.Iterable<? extends V> values)
Stores a collection of values with the same key, replacing any existing values for that key.private static <K,V>
voidsucceedsInMultimap(LinkedHashMultimap.ValueEntry<K,V> pred, LinkedHashMultimap.ValueEntry<K,V> succ)
private static <K,V>
voidsucceedsInValueSet(LinkedHashMultimap.ValueSetLink<K,V> pred, LinkedHashMultimap.ValueSetLink<K,V> succ)
(package private) java.util.Iterator<V>
valueIterator()
java.util.Collection<V>
values()
Returns a collection of all values in the multimap.private void
writeObject(java.io.ObjectOutputStream stream)
-
Methods inherited from class com.google.common.collect.AbstractSetMultimap
asMap, createUnmodifiableEmptyCollection, equals, get, put, removeAll
-
Methods inherited from class com.google.common.collect.AbstractMapBasedMultimap
backingMap, containsKey, createAsMap, createKeySet, setMap, size, unmodifiableCollectionSubclass, wrapCollection
-
Methods inherited from class com.google.common.collect.AbstractMultimap
containsEntry, containsValue, createEntries, createKeys, createValues, 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, hashCode, isEmpty, keys, putAll, putAll, remove, size
-
-
-
-
Field Detail
-
DEFAULT_KEY_CAPACITY
private static final int DEFAULT_KEY_CAPACITY
- See Also:
- Constant Field Values
-
DEFAULT_VALUE_SET_CAPACITY
private static final int DEFAULT_VALUE_SET_CAPACITY
- See Also:
- Constant Field Values
-
VALUE_SET_LOAD_FACTOR
static final double VALUE_SET_LOAD_FACTOR
- See Also:
- Constant Field Values
-
valueSetCapacity
transient int valueSetCapacity
-
multimapHeaderEntry
private transient LinkedHashMultimap.ValueEntry<K,V> multimapHeaderEntry
-
serialVersionUID
@GwtIncompatible private static final long serialVersionUID
- See Also:
- Constant Field Values
-
-
Method Detail
-
create
public static <K,V> LinkedHashMultimap<K,V> create()
Creates a new, emptyLinkedHashMultimap
with the default initial capacities.
-
create
public 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.- 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> LinkedHashMultimap<K,V> create(Multimap<? extends K,? extends V> multimap)
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
private static <K,V> void deleteFromValueSet(LinkedHashMultimap.ValueSetLink<K,V> entry)
-
deleteFromMultimap
private static <K,V> void deleteFromMultimap(LinkedHashMultimap.ValueEntry<K,V> entry)
-
createCollection
java.util.Set<V> 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
java.util.Collection<V> createCollection(K key)
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
public java.util.Set<V> replaceValues(@Nullable K key, java.lang.Iterable<? extends V> values)
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
public java.util.Set<java.util.Map.Entry<K,V>> 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
public java.util.Set<K> 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
public java.util.Collection<V> 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
java.util.Iterator<java.util.Map.Entry<K,V>> 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
-
valueIterator
java.util.Iterator<V> valueIterator()
- Overrides:
valueIterator
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
@GwtIncompatible private void writeObject(java.io.ObjectOutputStream stream) throws java.io.IOException
- Throws:
java.io.IOException
-
readObject
@GwtIncompatible private void readObject(java.io.ObjectInputStream stream) throws java.io.IOException, java.lang.ClassNotFoundException
- Throws:
java.io.IOException
java.lang.ClassNotFoundException
-
-