Class TreeMultimap<K,V>
- All Implemented Interfaces:
Multimap<K,
,V> SetMultimap<K,
,V> SortedSetMultimap<K,
,V> Serializable
Multimap
whose keys and values are ordered by their natural ordering or
by supplied comparators. In all cases, this implementation uses Comparable.compareTo(T)
or
Comparator.compare(T, T)
instead of Object.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 of SetMultimap
, which is specified in terms of Object.equals(java.lang.Object)
.
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(com.google.common.collect.SortedSetMultimap<K, V>)
.
See the Guava User Guide article on Multimap
.
- Since:
- 2.0
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.google.common.collect.AbstractMapBasedMultimap
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
FieldsModifier and TypeFieldDescriptionprivate Comparator
<? super K> private static final long
private Comparator
<? super V> -
Constructor Summary
ConstructorsModifierConstructorDescription(package private)
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 TypeMethodDescriptionasMap()
Returns a map view that associates each key with the corresponding values in the multimap.static <K extends Comparable,
V extends Comparable>
TreeMultimap<K, V> create()
Creates an emptyTreeMultimap
ordered by the natural ordering of its keys and values.static <K extends Comparable,
V extends Comparable>
TreeMultimap<K, V> 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
(Comparator<? super K> keyComparator, Comparator<? super V> valueComparator) Creates an emptyTreeMultimap
instance using explicit comparators.(package private) Map
<K, Collection<V>> Creates the collection of values for a single key.(package private) Collection
<V> createCollection
(K key) Creates the collection of values for an explicitly provided key.Returns a collection view of all values associated with a key.Comparator
<? super K> Deprecated.keySet()
Returns a view collection of all distinct keys contained in this multimap.private void
readObject
(ObjectInputStream stream) Comparator
<? super V> Returns the comparator that orders the multimap values, withnull
indicating that natural ordering is used.private void
writeObject
(ObjectOutputStream stream) Methods inherited from class com.google.common.collect.AbstractSortedKeySortedSetMultimap
backingMap, createKeySet
Methods inherited from class com.google.common.collect.AbstractSortedSetMultimap
createUnmodifiableEmptyCollection, removeAll, replaceValues, unmodifiableCollectionSubclass, values, wrapCollection
Methods inherited from class com.google.common.collect.AbstractSetMultimap
entries, equals, put
Methods inherited from class com.google.common.collect.AbstractMapBasedMultimap
clear, containsKey, createEntries, createKeys, createMaybeNavigableAsMap, createMaybeNavigableKeySet, createValues, entryIterator, entrySpliterator, forEach, setMap, size, valueIterator, valueSpliterator, 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
clear, containsEntry, containsKey, containsValue, forEach, hashCode, isEmpty, keys, put, putAll, putAll, remove, size
Methods inherited from interface com.google.common.collect.SetMultimap
entries, equals
-
Field Details
-
keyComparator
-
valueComparator
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
-
Constructor Details
-
TreeMultimap
TreeMultimap(Comparator<? super K> keyComparator, Comparator<? super V> valueComparator) -
TreeMultimap
private TreeMultimap(Comparator<? super K> keyComparator, Comparator<? super V> valueComparator, Multimap<? extends K, ? extends V> multimap)
-
-
Method Details
-
create
Creates an emptyTreeMultimap
ordered by the natural ordering of its keys and values. -
create
public static <K,V> TreeMultimap<K,V> create(Comparator<? super K> keyComparator, 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 Comparable,V extends 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
-
createAsMap
Map<K,Collection<V>> createAsMap()- Overrides:
createAsMap
in classAbstractMapBasedMultimap<K,
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
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
Deprecated.Use((NavigableSet<K>) multimap.keySet()).comparator()
instead.Returns the comparator that orders the multimap keys. -
valueComparator
Description copied from interface:SortedSetMultimap
Returns the comparator that orders the multimap values, withnull
indicating that natural ordering is used. -
get
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. -
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. -
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. -
writeObject
- Throws:
IOException
-
readObject
- Throws:
IOException
ClassNotFoundException
-
((NavigableSet<K>) multimap.keySet()).comparator()
instead.