Class TreeMultimap<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.AbstractSortedSetMultimap<K,V>
-
- com.google.common.collect.AbstractSortedKeySortedSetMultimap<K,V>
-
- com.google.common.collect.TreeMultimap<K,V>
-
- All Implemented Interfaces:
Multimap<K,V>
,SetMultimap<K,V>
,SortedSetMultimap<K,V>
,java.io.Serializable
@GwtCompatible(serializable=true, emulated=true) public class TreeMultimap<K,V> extends AbstractSortedKeySortedSetMultimap<K,V>
Implementation ofMultimap
whose keys and values are ordered by their natural ordering or by supplied comparators. In all cases, this implementation usesComparable.compareTo(T)
orComparator.compare(T, T)
instead ofObject.equals(java.lang.Object)
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 ofSetMultimap
, which it is specified in terms ofObject.equals(java.lang.Object)
.The collections returned by
keySet
andasMap
iterate through the keys according to the key comparator ordering or the natural ordering of the keys. Similarly,get
,removeAll
, andreplaceValues
return collections that iterate through the values according to the value comparator ordering or the natural ordering of the values. The collections generated byentries
,keys
, andvalues
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(com.google.common.collect.SortedSetMultimap<K, V>)
.See the Guava User Guide article on
Multimap
.- Since:
- 2.0
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
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 java.util.Comparator<? super K>
keyComparator
private static long
serialVersionUID
private java.util.Comparator<? super V>
valueComparator
-
Constructor Summary
Constructors Modifier Constructor Description (package private)
TreeMultimap(java.util.Comparator<? super K> keyComparator, java.util.Comparator<? super V> valueComparator)
private
TreeMultimap(java.util.Comparator<? super K> keyComparator, java.util.Comparator<? super V> valueComparator, Multimap<? extends K,? extends V> multimap)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.NavigableMap<K,java.util.Collection<V>>
asMap()
Returns a map view that associates each key with the corresponding values in the multimap.(package private) java.util.NavigableMap<K,java.util.Collection<V>>
backingMap()
static <K extends java.lang.Comparable,V extends java.lang.Comparable>
TreeMultimap<K,V>create()
Creates an emptyTreeMultimap
ordered by the natural ordering of its keys and values.static <K extends java.lang.Comparable,V extends java.lang.Comparable>
TreeMultimap<K,V>create(Multimap<? extends K,? extends V> multimap)
Constructs aTreeMultimap
, ordered by the natural ordering of its keys and values, with the same mappings as the specified multimap.static <K,V>
TreeMultimap<K,V>create(java.util.Comparator<? super K> keyComparator, java.util.Comparator<? super V> valueComparator)
Creates an emptyTreeMultimap
instance using explicit comparators.(package private) java.util.NavigableMap<K,java.util.Collection<V>>
createAsMap()
(package private) java.util.SortedSet<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.(package private) java.util.NavigableSet<K>
createKeySet()
java.util.NavigableSet<V>
get(K key)
Returns a collection view of all values associated with a key.java.util.Comparator<? super K>
keyComparator()
Returns the comparator that orders the multimap keys.java.util.NavigableSet<K>
keySet()
Returns a view collection of all distinct keys contained in this multimap.private void
readObject(java.io.ObjectInputStream stream)
(package private) java.util.Collection<V>
unmodifiableCollectionSubclass(java.util.Collection<V> collection)
java.util.Comparator<? super V>
valueComparator()
Returns the comparator that orders the multimap values, withnull
indicating that natural ordering is used.(package private) java.util.Collection<V>
wrapCollection(K key, java.util.Collection<V> collection)
Generates a decorated collection that remains consistent with the values in the multimap for the provided key.private void
writeObject(java.io.ObjectOutputStream stream)
-
Methods inherited from class com.google.common.collect.AbstractSortedSetMultimap
createUnmodifiableEmptyCollection, removeAll, replaceValues, values
-
Methods inherited from class com.google.common.collect.AbstractSetMultimap
entries, equals, put
-
Methods inherited from class com.google.common.collect.AbstractMapBasedMultimap
clear, containsKey, entryIterator, setMap, size, valueIterator
-
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
clear, containsEntry, containsKey, containsValue, hashCode, isEmpty, keys, put, putAll, putAll, remove, size
-
Methods inherited from interface com.google.common.collect.SetMultimap
entries, equals
-
-
-
-
Field Detail
-
keyComparator
private transient java.util.Comparator<? super K> keyComparator
-
valueComparator
private transient java.util.Comparator<? super V> valueComparator
-
serialVersionUID
@GwtIncompatible private static final long serialVersionUID
- See Also:
- Constant Field Values
-
-
Method Detail
-
create
public static <K extends java.lang.Comparable,V extends java.lang.Comparable> TreeMultimap<K,V> create()
Creates an emptyTreeMultimap
ordered by the natural ordering of its keys and values.
-
create
public static <K,V> TreeMultimap<K,V> create(java.util.Comparator<? super K> keyComparator, java.util.Comparator<? super V> valueComparator)
Creates an emptyTreeMultimap
instance using explicit comparators. Neither comparator may be null; useOrdering.natural()
to specify natural order.- Parameters:
keyComparator
- the comparator that determines the key orderingvalueComparator
- the comparator that determines the value ordering
-
create
public static <K extends java.lang.Comparable,V extends java.lang.Comparable> TreeMultimap<K,V> create(Multimap<? extends K,? extends V> multimap)
Constructs aTreeMultimap
, ordered by the natural ordering of its keys and values, with the same mappings as the specified multimap.- Parameters:
multimap
- the multimap whose contents are copied to this multimap
-
createCollection
java.util.SortedSet<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
TreeSet
for a collection of values for one key.- Specified by:
createCollection
in classAbstractSortedSetMultimap<K,V>
- Returns:
- a new
TreeSet
containing a collection of values for one key
-
createCollection
java.util.Collection<V> createCollection(@Nullable K key)
Description copied from class:AbstractMapBasedMultimap
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.- Overrides:
createCollection
in classAbstractMapBasedMultimap<K,V>
- Parameters:
key
- key to associate with values in the collection- Returns:
- an empty collection of values
-
keyComparator
public java.util.Comparator<? super K> keyComparator()
Returns the comparator that orders the multimap keys.
-
valueComparator
public java.util.Comparator<? super V> valueComparator()
Description copied from interface:SortedSetMultimap
Returns the comparator that orders the multimap values, withnull
indicating that natural ordering is used.
-
backingMap
@GwtIncompatible java.util.NavigableMap<K,java.util.Collection<V>> backingMap()
- Overrides:
backingMap
in classAbstractSortedKeySortedSetMultimap<K,V>
-
get
@GwtIncompatible public java.util.NavigableSet<V> get(@Nullable K key)
Description copied from class:AbstractSortedSetMultimap
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 aSortedSet
, instead of theCollection
specified in theMultimap
interface.
-
unmodifiableCollectionSubclass
@GwtIncompatible java.util.Collection<V> unmodifiableCollectionSubclass(java.util.Collection<V> collection)
- Overrides:
unmodifiableCollectionSubclass
in classAbstractMapBasedMultimap<K,V>
-
wrapCollection
@GwtIncompatible java.util.Collection<V> wrapCollection(K key, java.util.Collection<V> collection)
Description copied from class:AbstractMapBasedMultimap
Generates a decorated collection that remains consistent with the values in the multimap for the provided key. Changes to the multimap may alter the returned collection, and vice versa.- Overrides:
wrapCollection
in classAbstractMapBasedMultimap<K,V>
-
keySet
@GwtIncompatible public java.util.NavigableSet<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.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 aNavigableSet
, instead of theSet
specified in theMultimap
interface.
-
createKeySet
@GwtIncompatible java.util.NavigableSet<K> createKeySet()
- Overrides:
createKeySet
in classAbstractMapBasedMultimap<K,V>
-
asMap
@GwtIncompatible public java.util.NavigableMap<K,java.util.Collection<V>> 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 supportsetValue
on its entries,put
, orputAll
.When passed a key that is present in the map,
asMap().get(Object)
has the same behavior asAbstractSortedSetMultimap.get(K)
, returning a live collection. When passed a key that is not present, however,asMap().get(Object)
returnsnull
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 aNavigableMap
, instead of theMap
specified in theMultimap
interface.
-
createAsMap
@GwtIncompatible java.util.NavigableMap<K,java.util.Collection<V>> createAsMap()
- Overrides:
createAsMap
in classAbstractMapBasedMultimap<K,V>
-
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
-
-