@GwtCompatible(serializable=true, emulated=true) public class TreeMultimap<K,V> extends AbstractSortedKeySortedSetMultimap<K,V>
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 it 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.
AbstractMapBasedMultimap.NavigableAsMap, AbstractMapBasedMultimap.NavigableKeySet, AbstractMapBasedMultimap.WrappedNavigableSetAbstractMultimap.Values| Modifier and Type | Field and Description |
|---|---|
private java.util.Comparator<? super K> |
keyComparator |
private static long |
serialVersionUID |
private java.util.Comparator<? super V> |
valueComparator |
| Modifier | Constructor and 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) |
| Modifier and Type | Method and 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> |
create()
Creates an empty
TreeMultimap ordered by the natural ordering of
its keys and values. |
static <K,V> TreeMultimap<K,V> |
create(java.util.Comparator<? super K> keyComparator,
java.util.Comparator<? super V> valueComparator)
Creates an empty
TreeMultimap instance using explicit comparators. |
static <K extends java.lang.Comparable,V extends java.lang.Comparable> |
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. |
(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, with
null
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) |
createUnmodifiableEmptyCollection, removeAll, replaceValues, valuesentries, equals, putclear, containsKey, entryIterator, setMap, size, valueIteratorcontainsEntry, containsValue, createEntries, createKeys, createValues, hashCode, isEmpty, keys, putAll, putAll, remove, toStringclone, finalize, getClass, notify, notifyAll, wait, wait, waitentries, equalsclear, containsEntry, containsKey, containsValue, hashCode, isEmpty, keys, put, putAll, putAll, remove, sizeprivate transient java.util.Comparator<? super K> keyComparator
private transient java.util.Comparator<? super V> valueComparator
@GwtIncompatible private static final long serialVersionUID
TreeMultimap(java.util.Comparator<? super K> keyComparator, java.util.Comparator<? super V> valueComparator)
public static <K extends java.lang.Comparable,V extends java.lang.Comparable> TreeMultimap<K,V> create()
TreeMultimap ordered by the natural ordering of
its keys and values.public static <K,V> TreeMultimap<K,V> create(java.util.Comparator<? super K> keyComparator, java.util.Comparator<? super V> valueComparator)
TreeMultimap instance using explicit comparators.
Neither comparator may be null; use Ordering.natural() to specify
natural order.keyComparator - the comparator that determines the key orderingvalueComparator - the comparator that determines the value orderingpublic static <K extends java.lang.Comparable,V extends java.lang.Comparable> TreeMultimap<K,V> create(Multimap<? extends K,? extends V> multimap)
TreeMultimap, ordered by the natural ordering of its
keys and values, with the same mappings as the specified multimap.multimap - the multimap whose contents are copied to this multimapjava.util.SortedSet<V> createCollection()
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.
createCollection in class AbstractSortedSetMultimap<K,V>TreeSet containing a collection of values for one
keyjava.util.Collection<V> createCollection(@Nullable K key)
AbstractMapBasedMultimapAbstractMapBasedMultimap.createCollection(), which is the correct
behavior for most implementations. The LinkedHashMultimap class
overrides it.createCollection in class AbstractMapBasedMultimap<K,V>key - key to associate with values in the collectionpublic java.util.Comparator<? super K> keyComparator()
public java.util.Comparator<? super V> valueComparator()
SortedSetMultimapnull
indicating that natural ordering is used.@GwtIncompatible java.util.NavigableMap<K,java.util.Collection<V>> backingMap()
backingMap in class AbstractSortedKeySortedSetMultimap<K,V>@GwtIncompatible public java.util.NavigableSet<V> get(@Nullable K key)
AbstractSortedSetMultimapChanges 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.
@GwtIncompatible java.util.Collection<V> unmodifiableCollectionSubclass(java.util.Collection<V> collection)
unmodifiableCollectionSubclass in class AbstractMapBasedMultimap<K,V>@GwtIncompatible java.util.Collection<V> wrapCollection(K key, java.util.Collection<V> collection)
AbstractMapBasedMultimapwrapCollection in class AbstractMapBasedMultimap<K,V>@GwtIncompatible public java.util.NavigableSet<K> keySet()
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 Set specified
in the Multimap interface.
@GwtIncompatible java.util.NavigableSet<K> createKeySet()
createKeySet in class AbstractMapBasedMultimap<K,V>@GwtIncompatible public java.util.NavigableMap<K,java.util.Collection<V>> asMap()
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 AbstractSortedSetMultimap.get(K), 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 Map specified
in the Multimap interface.
@GwtIncompatible java.util.NavigableMap<K,java.util.Collection<V>> createAsMap()
createAsMap in class AbstractMapBasedMultimap<K,V>@GwtIncompatible private void writeObject(java.io.ObjectOutputStream stream) throws java.io.IOException
java.io.IOException@GwtIncompatible private void readObject(java.io.ObjectInputStream stream) throws java.io.IOException, java.lang.ClassNotFoundException
java.io.IOExceptionjava.lang.ClassNotFoundException