public final class LinkedHashMultimap<K,V> extends LinkedHashMultimapGwtSerializationDependencies<K,V>
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
.
Modifier and Type | Class and 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
Set<V> associated with a key, the linked list of insertion-ordered
entries in that Set<V> , and the linked list of entries in the LinkedHashMultimap as a
whole. |
(package private) class |
LinkedHashMultimap.ValueSet |
private static interface |
LinkedHashMultimap.ValueSetLink<K,V> |
AbstractMapBasedMultimap.NavigableAsMap, AbstractMapBasedMultimap.NavigableKeySet, AbstractMapBasedMultimap.WrappedCollection, AbstractMapBasedMultimap.WrappedList, AbstractMapBasedMultimap.WrappedNavigableSet, AbstractMapBasedMultimap.WrappedSet, AbstractMapBasedMultimap.WrappedSortedSet
AbstractMultimap.Entries, AbstractMultimap.EntrySet, AbstractMultimap.Values
Modifier and Type | Field and 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 |
Modifier | Constructor and Description |
---|---|
private |
LinkedHashMultimap(int keyCapacity,
int valueSetCapacity) |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes all key-value pairs from the multimap, leaving it empty.
|
static <K,V> LinkedHashMultimap<K,V> |
create()
Creates a new, empty
LinkedHashMultimap with the default initial capacities. |
static <K,V> LinkedHashMultimap<K,V> |
create(int expectedKeys,
int expectedValuesPerKey)
Constructs an empty
LinkedHashMultimap 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 a
LinkedHashMultimap 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> void |
deleteFromMultimap(LinkedHashMultimap.ValueEntry<K,V> entry) |
private static <K,V> void |
deleteFromValueSet(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 by
entries().iterator() and
values().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 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> 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) |
(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 void |
writeObject(java.io.ObjectOutputStream stream) |
asMap, createUnmodifiableEmptyCollection, equals, get, put, removeAll, unmodifiableCollectionSubclass, wrapCollection
backingMap, containsKey, createAsMap, createEntries, createKeys, createKeySet, createMaybeNavigableAsMap, createMaybeNavigableKeySet, createValues, forEach, setMap, size, wrapList
containsEntry, containsValue, hashCode, isEmpty, keys, putAll, putAll, remove, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
containsEntry, containsKey, containsValue, forEach, hashCode, isEmpty, keys, putAll, putAll, remove, size
private static final int DEFAULT_KEY_CAPACITY
private static final int DEFAULT_VALUE_SET_CAPACITY
static final double VALUE_SET_LOAD_FACTOR
transient int valueSetCapacity
private transient LinkedHashMultimap.ValueEntry<K,V> multimapHeaderEntry
private static final long serialVersionUID
private LinkedHashMultimap(int keyCapacity, int valueSetCapacity)
public static <K,V> LinkedHashMultimap<K,V> create()
LinkedHashMultimap
with the default initial capacities.public static <K,V> LinkedHashMultimap<K,V> create(int expectedKeys, int expectedValuesPerKey)
LinkedHashMultimap
with enough capacity to hold the specified
numbers of keys and values without rehashing.expectedKeys
- the expected number of distinct keysexpectedValuesPerKey
- the expected average number of values per keyjava.lang.IllegalArgumentException
- if expectedKeys
or expectedValuesPerKey
is
negativepublic static <K,V> LinkedHashMultimap<K,V> create(Multimap<? extends K,? extends V> multimap)
LinkedHashMultimap
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 same Multimap.entries()
iteration order
as the input multimap, except for excluding duplicate mappings.multimap
- the multimap whose contents are copied to this multimapprivate static <K,V> void succeedsInValueSet(LinkedHashMultimap.ValueSetLink<K,V> pred, LinkedHashMultimap.ValueSetLink<K,V> succ)
private static <K,V> void succeedsInMultimap(LinkedHashMultimap.ValueEntry<K,V> pred, LinkedHashMultimap.ValueEntry<K,V> succ)
private static <K,V> void deleteFromValueSet(LinkedHashMultimap.ValueSetLink<K,V> entry)
private static <K,V> void deleteFromMultimap(LinkedHashMultimap.ValueEntry<K,V> entry)
java.util.Set<V> createCollection()
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.
createCollection
in class AbstractSetMultimap<K,V>
LinkedHashSet
containing a collection of values for one keyjava.util.Collection<V> createCollection(K key)
AbstractMapBasedMultimap.createCollection()
, which is the correct behavior for most implementations. The LinkedHashMultimap
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.
createCollection
in class AbstractMapBasedMultimap<K,V>
key
- key to associate with values in the collectionpublic java.util.Set<V> replaceValues(K key, java.lang.Iterable<? extends V> values)
If values
is empty, this is equivalent to removeAll(key)
.
The returned collection is immutable.
Because a SetMultimap
has unique values for a given key, this method returns a
Set
, instead of the Collection
specified in the Multimap
interface.
Any duplicates in values
will be stored in the multimap once.
If values
is not empty and the multimap already contains a mapping for key
,
the keySet()
ordering is unchanged. However, the provided values always come last in
the entries()
and values()
iteration orderings.
replaceValues
in interface Multimap<K,V>
replaceValues
in interface SetMultimap<K,V>
replaceValues
in class AbstractSetMultimap<K,V>
public java.util.Set<java.util.Map.Entry<K,V>> entries()
add
or addAll
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.
public java.util.Set<K> keySet()
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.
public java.util.Collection<V> values()
The iterator generated by the returned collection traverses the values in the order they were added to the multimap.
java.util.Iterator<java.util.Map.Entry<K,V>> entryIterator()
AbstractMapBasedMultimap
entries().iterator()
and
values().iterator()
. The default behavior, which traverses the values for one key, the
values for a second key, and so on, suffices for most AbstractMapBasedMultimap
implementations.entryIterator
in class AbstractMapBasedMultimap<K,V>
java.util.Spliterator<java.util.Map.Entry<K,V>> entrySpliterator()
entrySpliterator
in class AbstractMapBasedMultimap<K,V>
java.util.Iterator<V> valueIterator()
valueIterator
in class AbstractMapBasedMultimap<K,V>
java.util.Spliterator<V> valueSpliterator()
valueSpliterator
in class AbstractMapBasedMultimap<K,V>
public void clear()
Multimap
private void writeObject(java.io.ObjectOutputStream stream) throws java.io.IOException
java.io.IOException
private void readObject(java.io.ObjectInputStream stream) throws java.io.IOException, java.lang.ClassNotFoundException
java.io.IOException
java.lang.ClassNotFoundException