Top Description Fields Constructors Methods
com.google.common.collect

public Class TreeMultimap<K extends @Nullable Object, V extends @Nullable Object>

extends AbstractSortedKeySortedSetMultimap<K extends @Nullable Object, V extends @Nullable Object>
Class Inheritance
Annotations
@GwtCompatible
serializable:true
emulated:true
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkNotNull

Implementation of Multimap whose keys and values are ordered by their natural ordering or by supplied comparators. In all cases, this implementation uses Comparable#compareTo or Comparator#compare instead of Object#equals to determine equivalence of instances.

Warning

The comparators or comparables used must be consistent with equals as explained by the Comparable class specification. Otherwise, the resulting multiset will violate the general contract of SetMultimap, which is specified in terms of Object#equals.

The collections returned by keySet and asMap iterate through the keys according to the key comparator ordering or the natural ordering of the keys. Similarly, get, removeAll, and replaceValues return collections that iterate through the values according to the value comparator ordering or the natural ordering of the values. The collections generated by entries, keys, and values iterate across the keys according to the above key ordering, and for each key they iterate across the values according to the value ordering.

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.

Null keys and values are permitted (provided, of course, that the respective comparators support them). 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#synchronizedSortedSetMultimap.

See the Guava User Guide article on Multimap.

Authors
Jared Levy, Louis Wasserman
Since
2.0

Field Summary

Modifier and TypeField and Description
private transient Comparator<? super K>
private static final long
private transient Comparator<? super V>

Constructor Summary

AccessConstructor and Description
pack-priv
TreeMultimap(Comparator<? super K> keyComparator, Comparator<? super V> valueComparator)

private
TreeMultimap(Comparator<? super K> keyComparator, Comparator<? super V> valueComparator, Multimap<? extends K, ? extends V> multimap)

Method Summary

Modifier and TypeMethod and Description
public NavigableMap<K, Collection<V>>
public static <K extends Comparable<T>, V extends Comparable<T>> TreeMultimap<K, V>
create()

Creates an empty TreeMultimap ordered by the natural ordering of its keys and values.

public static <K extends @Nullable Object, V extends @Nullable Object> TreeMultimap<K, V>
create(Comparator<? super K>
the comparator that determines the key ordering
keyComparator
,
Comparator<? super V>
the comparator that determines the value ordering
valueComparator
)

Creates an empty TreeMultimap instance using explicit comparators.

public static <K extends Comparable<T>, V extends Comparable<T>> TreeMultimap<K, V>
create(Multimap<? extends K, ? extends V>
the multimap whose contents are copied to this multimap
multimap
)

Constructs a TreeMultimap, ordered by the natural ordering of its keys and values, with the same mappings as the specified multimap.

pack-priv Map<K, Collection<V>>
pack-priv SortedSet<V>

Returns:

a new TreeSet containing a collection of values for one key
createCollection
()

Implements abstract com.google.common.collect.AbstractSortedSetMultimap.createCollection.

Creates the collection of values for a single key.
pack-priv Collection<V>
createCollection(K
key to associate with values in the collection
key
)

Overrides com.google.common.collect.AbstractMapBasedMultimap.createCollection.

Creates the collection of values for an explicitly provided key.
public NavigableSet<V>
public Comparator<? super K>
keyComparator()

Deprecated Use ((NavigableSet<K>) multimap.keySet()).comparator() instead.
Returns the comparator that orders the multimap keys.
public NavigableSet<K>
keySet()

Overrides com.google.common.collect.AbstractSortedKeySortedSetMultimap.keySet.

Implements com.google.common.collect.Multimap.keySet.

Returns a view collection of all distinct keys contained in this multimap.
private void
public Comparator<? super V>
valueComparator()

Implements com.google.common.collect.SortedSetMultimap.valueComparator.

Returns the comparator that orders the multimap values, with null indicating that natural ordering is used.
private void
Inherited from com.google.common.collect.AbstractSortedKeySortedSetMultimap:
backingMapcreateKeySet

Field Detail

keyComparatorback to summary
private transient Comparator<? super K> keyComparator
serialVersionUIDback to summary
private static final long serialVersionUID

Hides com.google.common.collect.AbstractSortedSetMultimap.serialVersionUID.

Annotations
@GwtIncompatible
@J2ktIncompatible
valueComparatorback to summary
private transient Comparator<? super V> valueComparator

Constructor Detail

TreeMultimapback to summary
pack-priv TreeMultimap(Comparator<? super K> keyComparator, Comparator<? super V> valueComparator)
TreeMultimapback to summary
private TreeMultimap(Comparator<? super K> keyComparator, Comparator<? super V> valueComparator, Multimap<? extends K, ? extends V> multimap)

Method Detail

asMapback to summary
public NavigableMap<K, Collection<V>> asMap()

Overrides com.google.common.collect.AbstractSortedKeySortedSetMultimap.asMap.

Implements com.google.common.collect.SortedSetMultimap.asMap, com.google.common.collect.SetMultimap.asMap, com.google.common.collect.Multimap.asMap.

Doc from com.google.common.collect.AbstractSortedSetMultimap.asMap.

Returns a map view that associates each key with the corresponding values in the multimap. Changes to the returned map, such as element removal, will update the underlying multimap. The map does not support setValue on its entries, put, or putAll.

When passed a key that is present in the map, asMap().get(Object) has the same behavior as get, returning a live collection. When passed a key that is not present, however, asMap().get(Object) returns null instead of an empty collection.

Though the method signature doesn't say so explicitly, the returned map has SortedSet values.

Because a TreeMultimap has unique sorted keys, this method returns a NavigableMap, instead of the java.util.Map specified in the Multimap interface.

Annotations
@Override
Since
14.0 (present with return type SortedMap since 2.0)
createback to summary
public static <K extends Comparable<T>, V extends Comparable<T>> TreeMultimap<K, V> create()

Creates an empty TreeMultimap ordered by the natural ordering of its keys and values.

createback to summary
public static <K extends @Nullable Object, V extends @Nullable Object> TreeMultimap<K, V> create(Comparator<? super K> keyComparator, Comparator<? super V> valueComparator)

Creates an empty TreeMultimap instance using explicit comparators. Neither comparator may be null; use Ordering#natural() to specify natural order.

Parameters
keyComparator:Comparator<? super K>

the comparator that determines the key ordering

valueComparator:Comparator<? super V>

the comparator that determines the value ordering

createback to summary
public static <K extends Comparable<T>, V extends Comparable<T>> TreeMultimap<K, V> create(Multimap<? extends K, ? extends V> multimap)

Constructs a TreeMultimap, ordered by the natural ordering of its keys and values, with the same mappings as the specified multimap.

Parameters
multimap:Multimap<? extends K, ? extends V>

the multimap whose contents are copied to this multimap

createAsMapback to summary
pack-priv Map<K, Collection<V>> createAsMap()

Overrides com.google.common.collect.AbstractMapBasedMultimap.createAsMap.

Annotations
@Override
createCollectionback to summary
pack-priv SortedSet<V> createCollection()

Implements abstract com.google.common.collect.AbstractSortedSetMultimap.createCollection.

Doc from com.google.common.collect.AbstractMapBasedMultimap.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 TreeSet for a collection of values for one key.

Returns:SortedSet<V>

a new TreeSet containing a collection of values for one key

Annotations
@Override
createCollectionback to summary
pack-priv Collection<V> createCollection(K key)

Overrides com.google.common.collect.AbstractMapBasedMultimap.createCollection.

Doc from com.google.common.collect.AbstractMapBasedMultimap.createCollection.

Creates the collection of values for an explicitly provided key. By default, it simply calls createCollection(), which is the correct behavior for most implementations. The LinkedHashMultimap class overrides it.

Parameters
key:K

key to associate with values in the collection

Returns:Collection<V>

an empty collection of values

Annotations
@Override
getback to summary
public NavigableSet<V> get(K key)

Overrides com.google.common.collect.AbstractSortedSetMultimap.get.

Implements com.google.common.collect.SortedSetMultimap.get, com.google.common.collect.SetMultimap.get, com.google.common.collect.Multimap.get.

Doc from com.google.common.collect.AbstractSortedSetMultimap.get.

Returns a collection view of all values associated with a key. If no mappings in the multimap have the provided key, an empty collection is returned.

Changes to the returned collection will update the underlying multimap, and vice versa.

Because a SortedSetMultimap has unique sorted values for a given key, this method returns a SortedSet, instead of the Collection specified in the Multimap interface.

Annotations
@Override
@GwtIncompatible
Since
14.0 (present with return type SortedSet since 2.0)
keyComparatorback to summary
public Comparator<? super K> keyComparator()

Deprecated

Use ((NavigableSet<K>) multimap.keySet()).comparator() instead.

Returns the comparator that orders the multimap keys.

Annotations
@Deprecated
keySetback to summary
public NavigableSet<K> keySet()

Overrides com.google.common.collect.AbstractSortedKeySortedSetMultimap.keySet.

Implements com.google.common.collect.Multimap.keySet.

Doc from com.google.common.collect.Multimap.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.

Changes to the returned set will update the underlying multimap, and vice versa. However, adding to the returned set is not possible.

Because a TreeMultimap has unique sorted keys, this method returns a NavigableSet, instead of the java.util.Set specified in the Multimap interface.

Annotations
@Override
Since
14.0 (present with return type SortedSet since 2.0)
readObjectback to summary
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException
Annotations
@GwtIncompatible
@J2ktIncompatible
@SuppressWarnings:unchecked
valueComparatorback to summary
public Comparator<? super V> valueComparator()

Implements com.google.common.collect.SortedSetMultimap.valueComparator.

Doc from com.google.common.collect.SortedSetMultimap.valueComparator.

Returns the comparator that orders the multimap values, with null indicating that natural ordering is used.

Annotations
@Override
writeObjectback to summary
private void writeObject(ObjectOutputStream stream) throws IOException
Annotations
@GwtIncompatible
@J2ktIncompatible
Serial data
key comparator, value comparator, number of distinct keys, and then for each distinct key: the key, number of values for that key, and key values