Class ImmutableSortedMap<K,V>

All Implemented Interfaces:
Serializable, Map<K,V>, NavigableMap<K,V>, SequencedMap<K,V>, SortedMap<K,V>

public final class ImmutableSortedMap<K,V> extends ImmutableSortedMapFauxverideShim<K,V> implements NavigableMap<K,V>
A NavigableMap whose contents will never change, with many other important properties detailed at ImmutableCollection.

Warning: as with any sorted collection, you are strongly advised not to use a Comparator or Comparable type whose comparison behavior is inconsistent with equals. That is, a.compareTo(b) or comparator.compare(a, b) should equal zero if and only if a.equals(b). If this advice is not followed, the resulting map will not correctly obey its specification.

See the Guava User Guide article on immutable collections.

Since:
2.0 (implements NavigableMap since 12.0)
See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • toImmutableSortedMap

      public static <T, K, V> Collector<T,?,ImmutableSortedMap<K,V>> toImmutableSortedMap(Comparator<? super K> comparator, Function<? super T,? extends K> keyFunction, Function<? super T,? extends V> valueFunction)
      Returns a Collector that accumulates elements into an ImmutableSortedMap whose keys and values are the result of applying the provided mapping functions to the input elements. The generated map is sorted by the specified comparator.

      If the mapped keys contain duplicates (according to the specified comparator), an IllegalArgumentException is thrown when the collection operation is performed. (This differs from the Collector returned by Collectors.toMap(Function, Function), which throws an IllegalStateException.)

      Since:
      21.0
    • toImmutableSortedMap

      public static <T, K, V> Collector<T,?,ImmutableSortedMap<K,V>> toImmutableSortedMap(Comparator<? super K> comparator, Function<? super T,? extends K> keyFunction, Function<? super T,? extends V> valueFunction, BinaryOperator<V> mergeFunction)
      Returns a Collector that accumulates elements into an ImmutableSortedMap whose keys and values are the result of applying the provided mapping functions to the input elements.

      If the mapped keys contain duplicates (according to the comparator), the values are merged using the specified merging function. Entries will appear in the encounter order of the first occurrence of the key.

      Since:
      21.0
    • emptyMap

      static <K, V> ImmutableSortedMap<K,V> emptyMap(Comparator<? super K> comparator)
    • of

      public static <K, V> ImmutableSortedMap<K,V> of()
      Returns the empty sorted map.

      Performance note: the instance returned is a singleton.

    • of

      public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K,V> of(K k1, V v1)
      Returns an immutable map containing a single entry.
    • of

      private static <K, V> ImmutableSortedMap<K,V> of(Comparator<? super K> comparator, K k1, V v1)
      Returns an immutable map containing a single entry.
    • of

      public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K,V> of(K k1, V v1, K k2, V v2)
      Returns an immutable sorted map containing the given entries, sorted by the natural ordering of their keys.
      Throws:
      IllegalArgumentException - if the two keys are equal according to their natural ordering
    • of

      public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3)
      Returns an immutable sorted map containing the given entries, sorted by the natural ordering of their keys.
      Throws:
      IllegalArgumentException - if any two keys are equal according to their natural ordering
    • of

      public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
      Returns an immutable sorted map containing the given entries, sorted by the natural ordering of their keys.
      Throws:
      IllegalArgumentException - if any two keys are equal according to their natural ordering
    • of

      public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)
      Returns an immutable sorted map containing the given entries, sorted by the natural ordering of their keys.
      Throws:
      IllegalArgumentException - if any two keys are equal according to their natural ordering
    • of

      public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6)
      Returns an immutable sorted map containing the given entries, sorted by the natural ordering of their keys.
      Throws:
      IllegalArgumentException - if any two keys are equal according to their natural ordering
      Since:
      31.0
    • of

      public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7)
      Returns an immutable sorted map containing the given entries, sorted by the natural ordering of their keys.
      Throws:
      IllegalArgumentException - if any two keys are equal according to their natural ordering
      Since:
      31.0
    • of

      public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8)
      Returns an immutable sorted map containing the given entries, sorted by the natural ordering of their keys.
      Throws:
      IllegalArgumentException - if any two keys are equal according to their natural ordering
      Since:
      31.0
    • of

      public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9)
      Returns an immutable sorted map containing the given entries, sorted by the natural ordering of their keys.
      Throws:
      IllegalArgumentException - if any two keys are equal according to their natural ordering
      Since:
      31.0
    • of

      public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10)
      Returns an immutable sorted map containing the given entries, sorted by the natural ordering of their keys.
      Throws:
      IllegalArgumentException - if any two keys are equal according to their natural ordering
      Since:
      31.0
    • copyOf

      public static <K, V> ImmutableSortedMap<K,V> copyOf(Map<? extends K,? extends V> map)
      Returns an immutable map containing the same entries as map, sorted by the natural ordering of the keys.

      Despite the method name, this method attempts to avoid actually copying the data when it is safe to do so. The exact circumstances under which a copy will or will not be performed are undocumented and subject to change.

      This method is not type-safe, as it may be called on a map with keys that are not mutually comparable.

      Throws:
      ClassCastException - if the keys in map are not mutually comparable
      NullPointerException - if any key or value in map is null
      IllegalArgumentException - if any two keys are equal according to their natural ordering
    • copyOf

      public static <K, V> ImmutableSortedMap<K,V> copyOf(Map<? extends K,? extends V> map, Comparator<? super K> comparator)
      Returns an immutable map containing the same entries as map, with keys sorted by the provided comparator.

      Despite the method name, this method attempts to avoid actually copying the data when it is safe to do so. The exact circumstances under which a copy will or will not be performed are undocumented and subject to change.

      Throws:
      NullPointerException - if any key or value in map is null
      IllegalArgumentException - if any two keys are equal according to the comparator
    • copyOf

      public static <K, V> ImmutableSortedMap<K,V> copyOf(Iterable<? extends Map.Entry<? extends K,? extends V>> entries)
      Returns an immutable map containing the given entries, with keys sorted by their natural ordering.

      This method is not type-safe, as it may be called on a map with keys that are not mutually comparable.

      Throws:
      NullPointerException - if any key or value in map is null
      IllegalArgumentException - if any two keys are equal according to the comparator
      Since:
      19.0
    • copyOf

      public static <K, V> ImmutableSortedMap<K,V> copyOf(Iterable<? extends Map.Entry<? extends K,? extends V>> entries, Comparator<? super K> comparator)
      Returns an immutable map containing the given entries, with keys sorted by the provided comparator.
      Throws:
      NullPointerException - if any key or value in map is null
      IllegalArgumentException - if any two keys are equal according to the comparator
      Since:
      19.0
    • copyOfSorted

      public static <K, V> ImmutableSortedMap<K,V> copyOfSorted(SortedMap<K,? extends V> map)
      Returns an immutable map containing the same entries as the provided sorted map, with the same ordering.

      Despite the method name, this method attempts to avoid actually copying the data when it is safe to do so. The exact circumstances under which a copy will or will not be performed are undocumented and subject to change.

      Throws:
      NullPointerException - if any key or value in map is null
    • copyOfInternal

      private static <K, V> ImmutableSortedMap<K,V> copyOfInternal(Map<? extends K,? extends V> map, Comparator<? super K> comparator)
    • fromEntries

      private static <K extends Comparable<? super K>, V> ImmutableSortedMap<K,V> fromEntries(Map.Entry<K,V>... entries)
    • fromEntries

      private static <K, V> ImmutableSortedMap<K,V> fromEntries(Comparator<? super K> comparator, boolean sameComparator, Iterable<? extends Map.Entry<? extends K,? extends V>> entries)
      Accepts a collection of possibly-null entries. If sameComparator, then it is assumed that they do not need to be sorted or checked for dupes.
    • fromEntries

      private static <K, V> ImmutableSortedMap<K,V> fromEntries(Comparator<? super K> comparator, boolean sameComparator, Map.Entry<K,V>[] entryArray, int size)
    • naturalOrder

      public static <K extends Comparable<?>, V> ImmutableSortedMap.Builder<K,V> naturalOrder()
      Returns a builder that creates immutable sorted maps whose keys are ordered by their natural ordering. The sorted maps use Ordering.natural() as the comparator.
    • orderedBy

      public static <K, V> ImmutableSortedMap.Builder<K,V> orderedBy(Comparator<K> comparator)
      Returns a builder that creates immutable sorted maps with an explicit comparator. If the comparator has a more general type than the map's keys, such as creating a SortedMap<Integer, String> with a Comparator<Number>, use the ImmutableSortedMap.Builder constructor instead.
      Throws:
      NullPointerException - if comparator is null
    • reverseOrder

      public static <K extends Comparable<?>, V> ImmutableSortedMap.Builder<K,V> reverseOrder()
      Returns a builder that creates immutable sorted maps whose keys are ordered by the reverse of their natural ordering.
    • size

      public int size()
      Specified by:
      size in interface Map<K,V>
    • forEach

      public void forEach(BiConsumer<? super K,? super V> action)
      Specified by:
      forEach in interface Map<K,V>
    • get

      @CheckForNull public V get(@CheckForNull Object key)
      Specified by:
      get in interface Map<K,V>
      Specified by:
      get in class ImmutableMap<K,V>
    • isPartialView

      boolean isPartialView()
      Specified by:
      isPartialView in class ImmutableMap<K,V>
    • entrySet

      public ImmutableSet<Map.Entry<K,V>> entrySet()
      Returns an immutable set of the mappings in this map, sorted by the key ordering.
      Specified by:
      entrySet in interface Map<K,V>
      Specified by:
      entrySet in interface SortedMap<K,V>
      Overrides:
      entrySet in class ImmutableMap<K,V>
    • createEntrySet

      ImmutableSet<Map.Entry<K,V>> createEntrySet()
      Specified by:
      createEntrySet in class ImmutableMap<K,V>
    • keySet

      public ImmutableSortedSet<K> keySet()
      Returns an immutable sorted set of the keys in this map.
      Specified by:
      keySet in interface Map<K,V>
      Specified by:
      keySet in interface SortedMap<K,V>
      Overrides:
      keySet in class ImmutableMap<K,V>
    • createKeySet

      ImmutableSet<K> createKeySet()
      Specified by:
      createKeySet in class ImmutableMap<K,V>
    • values

      public ImmutableCollection<V> values()
      Returns an immutable collection of the values in this map, sorted by the ordering of the corresponding keys.
      Specified by:
      values in interface Map<K,V>
      Specified by:
      values in interface SortedMap<K,V>
      Overrides:
      values in class ImmutableMap<K,V>
    • createValues

      ImmutableCollection<V> createValues()
      Specified by:
      createValues in class ImmutableMap<K,V>
    • comparator

      public Comparator<? super K> comparator()
      Returns the comparator that orders the keys, which is Ordering.natural() when the natural ordering of the keys is used. Note that its behavior is not consistent with TreeMap.comparator(), which returns null to indicate natural ordering.
      Specified by:
      comparator in interface SortedMap<K,V>
    • firstKey

      public K firstKey()
      Specified by:
      firstKey in interface SortedMap<K,V>
    • lastKey

      public K lastKey()
      Specified by:
      lastKey in interface SortedMap<K,V>
    • getSubMap

      private ImmutableSortedMap<K,V> getSubMap(int fromIndex, int toIndex)
    • headMap

      public ImmutableSortedMap<K,V> headMap(K toKey)
      This method returns a ImmutableSortedMap, consisting of the entries whose keys are less than toKey.

      The SortedMap.headMap(K) documentation states that a submap of a submap throws an IllegalArgumentException if passed a toKey greater than an earlier toKey. However, this method doesn't throw an exception in that situation, but instead keeps the original toKey.

      Specified by:
      headMap in interface NavigableMap<K,V>
      Specified by:
      headMap in interface SortedMap<K,V>
    • headMap

      public ImmutableSortedMap<K,V> headMap(K toKey, boolean inclusive)
      This method returns a ImmutableSortedMap, consisting of the entries whose keys are less than (or equal to, if inclusive) toKey.

      The SortedMap.headMap(K) documentation states that a submap of a submap throws an IllegalArgumentException if passed a toKey greater than an earlier toKey. However, this method doesn't throw an exception in that situation, but instead keeps the original toKey.

      Specified by:
      headMap in interface NavigableMap<K,V>
      Since:
      12.0
    • subMap

      public ImmutableSortedMap<K,V> subMap(K fromKey, K toKey)
      This method returns a ImmutableSortedMap, consisting of the entries whose keys ranges from fromKey, inclusive, to toKey, exclusive.

      The SortedMap.subMap(K, K) documentation states that a submap of a submap throws an IllegalArgumentException if passed a fromKey less than an earlier fromKey. However, this method doesn't throw an exception in that situation, but instead keeps the original fromKey. Similarly, this method keeps the original toKey, instead of throwing an exception, if passed a toKey greater than an earlier toKey.

      Specified by:
      subMap in interface NavigableMap<K,V>
      Specified by:
      subMap in interface SortedMap<K,V>
    • subMap

      public ImmutableSortedMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)
      This method returns a ImmutableSortedMap, consisting of the entries whose keys ranges from fromKey to toKey, inclusive or exclusive as indicated by the boolean flags.

      The SortedMap.subMap(K, K) documentation states that a submap of a submap throws an IllegalArgumentException if passed a fromKey less than an earlier fromKey. However, this method doesn't throw an exception in that situation, but instead keeps the original fromKey. Similarly, this method keeps the original toKey, instead of throwing an exception, if passed a toKey greater than an earlier toKey.

      Specified by:
      subMap in interface NavigableMap<K,V>
      Since:
      12.0
    • tailMap

      public ImmutableSortedMap<K,V> tailMap(K fromKey)
      This method returns a ImmutableSortedMap, consisting of the entries whose keys are greater than or equals to fromKey.

      The SortedMap.tailMap(K) documentation states that a submap of a submap throws an IllegalArgumentException if passed a fromKey less than an earlier fromKey. However, this method doesn't throw an exception in that situation, but instead keeps the original fromKey.

      Specified by:
      tailMap in interface NavigableMap<K,V>
      Specified by:
      tailMap in interface SortedMap<K,V>
    • tailMap

      public ImmutableSortedMap<K,V> tailMap(K fromKey, boolean inclusive)
      This method returns a ImmutableSortedMap, consisting of the entries whose keys are greater than (or equal to, if inclusive) fromKey.

      The SortedMap.tailMap(K) documentation states that a submap of a submap throws an IllegalArgumentException if passed a fromKey less than an earlier fromKey. However, this method doesn't throw an exception in that situation, but instead keeps the original fromKey.

      Specified by:
      tailMap in interface NavigableMap<K,V>
      Since:
      12.0
    • lowerEntry

      @CheckForNull public Map.Entry<K,V> lowerEntry(K key)
      Specified by:
      lowerEntry in interface NavigableMap<K,V>
    • lowerKey

      @CheckForNull public K lowerKey(K key)
      Specified by:
      lowerKey in interface NavigableMap<K,V>
    • floorEntry

      @CheckForNull public Map.Entry<K,V> floorEntry(K key)
      Specified by:
      floorEntry in interface NavigableMap<K,V>
    • floorKey

      @CheckForNull public K floorKey(K key)
      Specified by:
      floorKey in interface NavigableMap<K,V>
    • ceilingEntry

      @CheckForNull public Map.Entry<K,V> ceilingEntry(K key)
      Specified by:
      ceilingEntry in interface NavigableMap<K,V>
    • ceilingKey

      @CheckForNull public K ceilingKey(K key)
      Specified by:
      ceilingKey in interface NavigableMap<K,V>
    • higherEntry

      @CheckForNull public Map.Entry<K,V> higherEntry(K key)
      Specified by:
      higherEntry in interface NavigableMap<K,V>
    • higherKey

      @CheckForNull public K higherKey(K key)
      Specified by:
      higherKey in interface NavigableMap<K,V>
    • firstEntry

      @CheckForNull public Map.Entry<K,V> firstEntry()
      Specified by:
      firstEntry in interface NavigableMap<K,V>
      Specified by:
      firstEntry in interface SequencedMap<K,V>
    • lastEntry

      @CheckForNull public Map.Entry<K,V> lastEntry()
      Specified by:
      lastEntry in interface NavigableMap<K,V>
      Specified by:
      lastEntry in interface SequencedMap<K,V>
    • pollFirstEntry

      @Deprecated @CheckForNull public final Map.Entry<K,V> pollFirstEntry()
      Deprecated.
      Unsupported operation.
      Guaranteed to throw an exception and leave the map unmodified.
      Specified by:
      pollFirstEntry in interface NavigableMap<K,V>
      Specified by:
      pollFirstEntry in interface SequencedMap<K,V>
      Throws:
      UnsupportedOperationException - always
    • pollLastEntry

      @Deprecated @CheckForNull public final Map.Entry<K,V> pollLastEntry()
      Deprecated.
      Unsupported operation.
      Guaranteed to throw an exception and leave the map unmodified.
      Specified by:
      pollLastEntry in interface NavigableMap<K,V>
      Specified by:
      pollLastEntry in interface SequencedMap<K,V>
      Throws:
      UnsupportedOperationException - always
    • descendingMap

      public ImmutableSortedMap<K,V> descendingMap()
      Specified by:
      descendingMap in interface NavigableMap<K,V>
    • descendingKeySet

      public ImmutableSortedSet<K> descendingKeySet()
      Specified by:
      descendingKeySet in interface NavigableMap<K,V>
    • writeReplace

      Object writeReplace()
      Description copied from class: ImmutableMap
      Returns a serializable form of this object. Non-public subclasses should not override this method. Publicly-accessible subclasses must override this method and should return a subclass of SerializedForm whose readResolve() method returns objects of the subclass type.
      Overrides:
      writeReplace in class ImmutableMap<K,V>
    • readObject

      private void readObject(ObjectInputStream stream) throws InvalidObjectException
      Throws:
      InvalidObjectException