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.LinkedHashMultimapGwtSerializationDependencies<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 LinkedHashMultimapGwtSerializationDependencies<K,V>
Implementation ofMultimapthat 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, andasMapiterate through the keys in the order they were first added to the multimap. Similarly,get,removeAll, andreplaceValuesreturn collections that iterate through the values in the order they were added. The collections generated byentriesandvaluesiterate across the key-value mappings in the order they were added to the multimap.The iteration ordering of the collections generated by
keySet,keys, andasMaphas 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 classLinkedHashMultimap.ValueEntry<K,V>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) classLinkedHashMultimap.ValueSetprivate static interfaceLinkedHashMultimap.ValueSetLink<K,V>-
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 intDEFAULT_KEY_CAPACITYprivate static intDEFAULT_VALUE_SET_CAPACITYprivate LinkedHashMultimap.ValueEntry<K,V>multimapHeaderEntryprivate static longserialVersionUID(package private) static doubleVALUE_SET_LOAD_FACTOR(package private) intvalueSetCapacity
-
Constructor Summary
Constructors Modifier Constructor Description privateLinkedHashMultimap(int keyCapacity, int valueSetCapacity)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Removes all key-value pairs from the multimap, leaving it empty.static <K,V>
LinkedHashMultimap<K,V>create()Creates a new, emptyLinkedHashMultimapwith the default initial capacities.static <K,V>
LinkedHashMultimap<K,V>create(int expectedKeys, int expectedValuesPerKey)Constructs an emptyLinkedHashMultimapwith 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 aLinkedHashMultimapwith 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().(package private) java.util.Spliterator<java.util.Map.Entry<K,V>>entrySpliterator()java.util.Set<K>keySet()Returns a view collection of all distinct keys contained in this multimap.private voidreadObject(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.(package private) java.util.Spliterator<V>valueSpliterator()private voidwriteObject(java.io.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 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, emptyLinkedHashMultimapwith the default initial capacities.
-
create
public static <K,V> LinkedHashMultimap<K,V> create(int expectedKeys, int expectedValuesPerKey)
Constructs an emptyLinkedHashMultimapwith 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- ifexpectedKeysorexpectedValuesPerKeyis negative
-
create
public static <K,V> LinkedHashMultimap<K,V> create(Multimap<? extends K,? extends V> multimap)
Constructs aLinkedHashMultimapwith 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
createCollectionshould create a new instance.The returned collection class determines whether duplicate key-value pairs are allowed.
Creates an empty
LinkedHashSetfor a collection of values for one key.- Specified by:
createCollectionin classAbstractSetMultimap<K,V>- Returns:
- a new
LinkedHashSetcontaining 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. TheLinkedHashMultimapclass 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:
createCollectionin 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(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
valuesis empty, this is equivalent toremoveAll(key).The returned collection is immutable.
Because a
SetMultimaphas unique values for a given key, this method returns aSet, instead of theCollectionspecified in theMultimapinterface.Any duplicates in
valueswill be stored in the multimap once.If
valuesis 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:
replaceValuesin interfaceMultimap<K,V>- Specified by:
replaceValuesin interfaceSetMultimap<K,V>- Overrides:
replaceValuesin 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 theaddoraddAlloperations.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:AbstractMapBasedMultimapReturns 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 mostAbstractMapBasedMultimapimplementations.- Overrides:
entryIteratorin classAbstractMapBasedMultimap<K,V>- Returns:
- an iterator across map entries
-
entrySpliterator
java.util.Spliterator<java.util.Map.Entry<K,V>> entrySpliterator()
- Overrides:
entrySpliteratorin classAbstractMapBasedMultimap<K,V>
-
valueIterator
java.util.Iterator<V> valueIterator()
- Overrides:
valueIteratorin classAbstractMapBasedMultimap<K,V>
-
valueSpliterator
java.util.Spliterator<V> valueSpliterator()
- Overrides:
valueSpliteratorin classAbstractMapBasedMultimap<K,V>
-
clear
public void clear()
Description copied from interface:MultimapRemoves 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.IOExceptionjava.lang.ClassNotFoundException
-
-