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.NavigableAsMap, AbstractMapBasedMultimap.NavigableKeySet, AbstractMapBasedMultimap.WrappedCollection, AbstractMapBasedMultimap.WrappedList, AbstractMapBasedMultimap.WrappedNavigableSet, AbstractMapBasedMultimap.WrappedSet, AbstractMapBasedMultimap.WrappedSortedSetNested 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 longprivate Comparator<? super V> -
Constructor Summary
ConstructorsModifierConstructorDescription(package private)TreeMultimap(Comparator<? super K> keyComparator, Comparator<? super V> valueComparator) privateTreeMultimap(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 emptyTreeMultimapordered 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 emptyTreeMultimapinstance 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 voidreadObject(ObjectInputStream stream) Comparator<? super V>Returns the comparator that orders the multimap values, withnullindicating that natural ordering is used.private voidwriteObject(ObjectOutputStream stream) Methods inherited from class com.google.common.collect.AbstractSortedKeySortedSetMultimap
backingMap, createKeySetMethods inherited from class com.google.common.collect.AbstractSortedSetMultimap
createUnmodifiableEmptyCollection, removeAll, replaceValues, unmodifiableCollectionSubclass, values, wrapCollectionMethods inherited from class com.google.common.collect.AbstractSetMultimap
entries, equals, putMethods inherited from class com.google.common.collect.AbstractMapBasedMultimap
clear, containsKey, createEntries, createKeys, createMaybeNavigableAsMap, createMaybeNavigableKeySet, createValues, entryIterator, entrySpliterator, forEach, setMap, size, valueIterator, valueSpliterator, wrapListMethods inherited from class com.google.common.collect.AbstractMultimap
containsEntry, containsValue, hashCode, isEmpty, keys, putAll, putAll, remove, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.google.common.collect.Multimap
clear, containsEntry, containsKey, containsValue, forEach, hashCode, isEmpty, keys, put, putAll, putAll, remove, sizeMethods 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 emptyTreeMultimapordered 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 emptyTreeMultimapinstance 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:
createAsMapin 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
createCollectionshould create a new instance.The returned collection class determines whether duplicate key-value pairs are allowed.
Creates an empty
TreeSetfor a collection of values for one key.- Specified by:
createCollectionin classAbstractSortedSetMultimap<K,V> - Returns:
- a new
TreeSetcontaining a collection of values for one key
-
createCollection
Description copied from class:AbstractMapBasedMultimapCreates the collection of values for an explicitly provided key. By default, it simply callsAbstractMapBasedMultimap.createCollection(), which is the correct behavior for most implementations. TheLinkedHashMultimapclass overrides it.- Overrides:
createCollectionin 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:SortedSetMultimapReturns the comparator that orders the multimap values, withnullindicating that natural ordering is used. -
get
Description copied from class:AbstractSortedSetMultimapReturns 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
SortedSetMultimaphas unique sorted values for a given key, this method returns aSortedSet, instead of theCollectionspecified in theMultimapinterface. -
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
TreeMultimaphas unique sorted keys, this method returns aNavigableSet, instead of theSetspecified in theMultimapinterface. -
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 supportsetValueon 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)returnsnullinstead of an empty collection.Though the method signature doesn't say so explicitly, the returned map has
SortedSetvalues.Because a
TreeMultimaphas unique sorted keys, this method returns aNavigableMap, instead of theMapspecified in theMultimapinterface. -
writeObject
- Throws:
IOException
-
readObject
- Throws:
IOExceptionClassNotFoundException
-
((NavigableSet<K>) multimap.keySet()).comparator()instead.