Class LinkedHashMultimap<K,V>

All Implemented Interfaces:
Multimap<K,V>, SetMultimap<K,V>, Serializable

public final class LinkedHashMultimap<K,V> extends LinkedHashMultimapGwtSerializationDependencies<K,V>
Implementation of 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:
  • Field Details

  • Constructor Details

    • LinkedHashMultimap

      private LinkedHashMultimap(int keyCapacity, int valueSetCapacity)
  • Method Details

    • create

      public static <K, V> LinkedHashMultimap<K,V> create()
      Creates a new, empty LinkedHashMultimap with the default initial capacities.
    • create

      public 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.
      Parameters:
      expectedKeys - the expected number of distinct keys
      expectedValuesPerKey - the expected average number of values per key
      Throws:
      IllegalArgumentException - if expectedKeys or expectedValuesPerKey is negative
    • create

      public static <K, V> LinkedHashMultimap<K,V> create(Multimap<? extends K,? extends V> multimap)
      Constructs a 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.
      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

      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 class AbstractSetMultimap<K,V>
      Returns:
      a new LinkedHashSet containing a collection of values for one key
    • createCollection

      Collection<V> createCollection(K key)
      Creates the collection of values for an explicitly provided key. By default, it simply calls 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.

      Overrides:
      createCollection in class AbstractMapBasedMultimap<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 Set<V> replaceValues(K key, 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 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.

      Specified by:
      replaceValues in interface Multimap<K,V>
      Specified by:
      replaceValues in interface SetMultimap<K,V>
      Overrides:
      replaceValues in class AbstractSetMultimap<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 Set<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 the 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.

      Specified by:
      entries in interface Multimap<K,V>
      Specified by:
      entries in interface SetMultimap<K,V>
      Overrides:
      entries in class AbstractSetMultimap<K,V>
    • keySet

      public 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.

      Specified by:
      keySet in interface Multimap<K,V>
      Overrides:
      keySet in class AbstractMultimap<K,V>
    • values

      public 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.

      Specified by:
      values in interface Multimap<K,V>
      Overrides:
      values in class AbstractMapBasedMultimap<K,V>
    • entryIterator

      Iterator<Map.Entry<K,V>> entryIterator()
      Description copied from class: AbstractMapBasedMultimap
      Returns an iterator across all key-value map entries, used by 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.
      Overrides:
      entryIterator in class AbstractMapBasedMultimap<K,V>
      Returns:
      an iterator across map entries
    • entrySpliterator

      Spliterator<Map.Entry<K,V>> entrySpliterator()
      Overrides:
      entrySpliterator in class AbstractMapBasedMultimap<K,V>
    • valueIterator

      Iterator<V> valueIterator()
      Overrides:
      valueIterator in class AbstractMapBasedMultimap<K,V>
    • valueSpliterator

      Spliterator<V> valueSpliterator()
      Overrides:
      valueSpliterator in class AbstractMapBasedMultimap<K,V>
    • clear

      public void clear()
      Description copied from interface: Multimap
      Removes all key-value pairs from the multimap, leaving it empty.
      Specified by:
      clear in interface Multimap<K,V>
      Overrides:
      clear in class AbstractMapBasedMultimap<K,V>
    • writeObject

      private void writeObject(ObjectOutputStream stream) throws IOException
      Throws:
      IOException
    • readObject

      private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException
      Throws:
      IOException
      ClassNotFoundException