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
public class TreeMultimap<K,V> extends AbstractSortedKeySortedSetMultimap<K,V>
Implementation ofMultimapwhose 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
Comparableclass specification. Otherwise, the resulting multiset will violate the general contract ofSetMultimap, which is specified in terms ofObject.equals(java.lang.Object).The collections returned by
keySetandasMapiterate through the keys according to the key comparator ordering or the natural ordering of the keys. Similarly,get,removeAll, andreplaceValuesreturn collections that iterate through the values according to the value comparator ordering or the natural ordering of the values. The collections generated byentries,keys, andvaluesiterate 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.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
Fields Modifier and Type Field Description private java.util.Comparator<? super K>keyComparatorprivate static longserialVersionUIDprivate 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)privateTreeMultimap(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 Deprecated 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.static <K extends java.lang.Comparable,V extends java.lang.Comparable>
TreeMultimap<K,V>create()Creates an emptyTreeMultimapordered 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 emptyTreeMultimapinstance using explicit comparators.(package private) java.util.Map<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.java.util.NavigableSet<V>get(K key)Returns a collection view of all values associated with a key.java.util.Comparator<? super K>keyComparator()Deprecated.Use((NavigableSet<K>) multimap.keySet()).comparator()instead.java.util.NavigableSet<K>keySet()Returns a view collection of all distinct keys contained in this multimap.private voidreadObject(java.io.ObjectInputStream stream)java.util.Comparator<? super V>valueComparator()Returns the comparator that orders the multimap values, withnullindicating that natural ordering is used.private voidwriteObject(java.io.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 Detail
-
keyComparator
private transient java.util.Comparator<? super K> keyComparator
-
valueComparator
private transient java.util.Comparator<? super V> valueComparator
-
serialVersionUID
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 emptyTreeMultimapordered 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 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 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
-
createAsMap
java.util.Map<K,java.util.Collection<V>> createAsMap()
- Overrides:
createAsMapin classAbstractMapBasedMultimap<K,V>
-
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
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
java.util.Collection<V> createCollection(K key)
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 public java.util.Comparator<? super K> keyComparator()
Deprecated.Use((NavigableSet<K>) multimap.keySet()).comparator()instead.Returns the comparator that orders the multimap keys.
-
valueComparator
public java.util.Comparator<? super V> valueComparator()
Description copied from interface:SortedSetMultimapReturns the comparator that orders the multimap values, withnullindicating that natural ordering is used.
-
get
public java.util.NavigableSet<V> get(K key)
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
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
TreeMultimaphas unique sorted keys, this method returns aNavigableSet, instead of theSetspecified in theMultimapinterface.
-
asMap
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 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
private void writeObject(java.io.ObjectOutputStream stream) throws java.io.IOException- Throws:
java.io.IOException
-
readObject
private void readObject(java.io.ObjectInputStream stream) throws java.io.IOException, java.lang.ClassNotFoundException- Throws:
java.io.IOExceptionjava.lang.ClassNotFoundException
-
-