Class 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 of 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.

    Since:
    2.0
    See Also:
    Serialized Form
    • Field Detail

      • keyComparator

        private transient java.util.Comparator<? super K> keyComparator
      • valueComparator

        private transient java.util.Comparator<? super V> valueComparator
    • Constructor Detail

      • TreeMultimap

        TreeMultimap​(java.util.Comparator<? super K> keyComparator,
                     java.util.Comparator<? super V> valueComparator)
      • TreeMultimap

        private TreeMultimap​(java.util.Comparator<? super K> keyComparator,
                             java.util.Comparator<? super V> valueComparator,
                             Multimap<? extends K,​? extends V> multimap)
    • Method Detail

      • create

        public static <K extends java.lang.Comparable,​V extends java.lang.Comparable> TreeMultimap<K,​V> create()
        Creates an empty TreeMultimap 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 empty TreeMultimap instance using explicit comparators. Neither comparator may be null; use Ordering.natural() to specify natural order.
        Parameters:
        keyComparator - the comparator that determines the key ordering
        valueComparator - 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 a TreeMultimap, 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 class AbstractSortedSetMultimap<K,​V>
        Returns:
        a new TreeSet containing a collection of values for one key
      • 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: SortedSetMultimap
        Returns the comparator that orders the multimap values, with null indicating that natural ordering is used.
      • get

        @GwtIncompatible
        public java.util.NavigableSet<V> get​(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 a SortedSet, instead of the Collection specified in the Multimap interface.

        Specified by:
        get in interface Multimap<K,​V>
        Specified by:
        get in interface SetMultimap<K,​V>
        Specified by:
        get in interface SortedSetMultimap<K,​V>
        Overrides:
        get in class AbstractSortedSetMultimap<K,​V>
        Since:
        14.0 (present with return type SortedSet since 2.0)
      • 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 TreeMultimap has unique sorted keys, this method returns a NavigableSet, instead of the Set specified in the Multimap interface.

        Specified by:
        keySet in interface Multimap<K,​V>
        Overrides:
        keySet in class AbstractSortedKeySortedSetMultimap<K,​V>
        Since:
        14.0 (present with return type SortedSet since 2.0)
      • 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 support 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.

        Specified by:
        asMap in interface Multimap<K,​V>
        Specified by:
        asMap in interface SetMultimap<K,​V>
        Specified by:
        asMap in interface SortedSetMultimap<K,​V>
        Overrides:
        asMap in class AbstractSortedKeySortedSetMultimap<K,​V>
        Since:
        14.0 (present with return type SortedMap since 2.0)
      • 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