@GwtCompatible(serializable=true, emulated=true) public final class ImmutableSortedMap<K,V> extends ImmutableMap<K ,V> implements NavigableMap <K ,V>
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.
NavigableMap since 12.0)
| Modifier and Type | Class and Description |
|---|---|
static class |
ImmutableSortedMap
A builder for creating immutable sorted map instances, especially
public static final maps ("constant maps").
|
| Modifier and Type | Method and Description |
|---|---|
static <K |
builder()
Deprecated.
Use
naturalOrder(), which offers better type-safety.
|
Map |
ceilingEntry(K key)
|
K |
ceilingKey(K key)
|
Comparator |
comparator()
Returns the comparator that orders the keys, which is
Ordering when the natural ordering of the keys is used.
|
static <K |
copyOf(Iterable
Returns an immutable map containing the given entries, with keys sorted by the provided comparator.
|
static <K |
copyOf(Iterable
Returns an immutable map containing the given entries, with keys sorted by the provided comparator.
|
static <K |
copyOf(Map
Returns an immutable map containing the same entries as
map, sorted by the natural ordering of the keys.
|
static <K |
copyOf(Map
Returns an immutable map containing the same entries as
map, with keys sorted by the provided comparator.
|
static <K |
copyOfSorted(SortedMap
Returns an immutable map containing the same entries as the provided sorted map, with the same ordering.
|
ImmutableSortedSet |
descendingKeySet()
|
ImmutableSortedMap |
descendingMap()
|
ImmutableSet |
entrySet()
Returns an immutable set of the mappings in this map, sorted by the key ordering.
|
Map |
firstEntry()
|
K |
firstKey()
|
Map |
floorEntry(K key)
|
K |
floorKey(K key)
|
V |
get(Object
|
ImmutableSortedMap |
headMap(K toKey)
This method returns a
ImmutableSortedMap, consisting of the entries whose keys are less than
toKey.
|
ImmutableSortedMap |
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.
|
Map |
higherEntry(K key)
|
K |
higherKey(K key)
|
ImmutableSortedSet |
keySet()
Returns an immutable sorted set of the keys in this map.
|
Map |
lastEntry()
|
K |
lastKey()
|
Map |
lowerEntry(K key)
|
K |
lowerKey(K key)
|
static <K extends Comparable |
naturalOrder()
Returns a builder that creates immutable sorted maps whose keys are ordered by their natural ordering.
|
ImmutableSortedSet |
navigableKeySet()
|
static <K |
of()
Returns the empty sorted map.
|
static <K extends Comparable |
of(K k1, V v1)
Returns an immutable map containing a single entry.
|
static <K extends Comparable |
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.
|
static <K extends Comparable |
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.
|
static <K extends Comparable |
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.
|
static <K extends Comparable |
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.
|
static <K |
orderedBy(Comparator
Returns a builder that creates immutable sorted maps with an explicit comparator.
|
Map |
pollFirstEntry()
Deprecated.
Unsupported operation.
|
Map |
pollLastEntry()
Deprecated.
Unsupported operation.
|
static <K extends Comparable |
reverseOrder()
Returns a builder that creates immutable sorted maps whose keys are ordered by the reverse of their natural ordering.
|
int |
size()
|
ImmutableSortedMap |
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.
|
ImmutableSortedMap |
subMap(K fromKey, K toKey)
This method returns a
ImmutableSortedMap, consisting of the entries whose keys ranges from
fromKey, inclusive, to
toKey, exclusive.
|
ImmutableSortedMap |
tailMap(K fromKey)
This method returns a
ImmutableSortedMap, consisting of the entries whose keys are greater than or equals to
fromKey.
|
ImmutableSortedMap |
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.
|
ImmutableCollection |
values()
Returns an immutable collection of the values in this map, sorted by the ordering of the corresponding keys.
|
asMultimap, clear, containsKey, containsValue, equals, hashCode, isEmpty, put, putAll, remove, toStringclone, finalize, getClass, notify, notifyAll, wait, wait, waitclear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, equals, forEach, getOrDefault, hashCode, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAllpublic static <K,V> ImmutableSortedMap <K ,V> of()
public static <K extends Comparable<? super K> ,V> ImmutableSortedMap <K ,V> of(K k1, V v1)
public static <K extends Comparable<? super K> ,V> ImmutableSortedMap <K ,V> of(K k1, V v1, K k2, V v2)
IllegalArgumentException - if the two keys are equal according to their natural ordering
public static <K extends Comparable<? super K> ,V> ImmutableSortedMap <K ,V> of(K k1, V v1, K k2, V v2, K k3, V v3)
IllegalArgumentException - if any two keys are equal according to their natural ordering
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)
IllegalArgumentException - if any two keys are equal according to their natural ordering
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)
IllegalArgumentException - if any two keys are equal according to their natural ordering
public static <K,V> ImmutableSortedMap <K ,V> copyOf(Map <? extends K ,? extends V> map)
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.
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
public static <K,V> ImmutableSortedMap <K ,V> copyOf(Map <? extends K ,? extends V> map, Comparator <? super K> comparator)
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.
NullPointerException - if any key or value in
map is null
IllegalArgumentException - if any two keys are equal according to the comparator
@Beta public static <K,V> ImmutableSortedMap <K ,V> copyOf(Iterable <? extends Map .Entry <? extends K ,? extends V>> entries)
This method is not type-safe, as it may be called on a map with keys that are not mutually comparable.
NullPointerException - if any key or value in
map is null
IllegalArgumentException - if any two keys are equal according to the comparator
@Beta public static <K,V> ImmutableSortedMap <K ,V> copyOf(Iterable <? extends Map .Entry <? extends K ,? extends V>> entries, Comparator <? super K> comparator)
NullPointerException - if any key or value in
map is null
IllegalArgumentException - if any two keys are equal according to the comparator
public static <K,V> ImmutableSortedMap <K ,V> copyOfSorted(SortedMap <K ,? extends V> map)
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.
NullPointerException - if any key or value in
map is null
public static <K extends Comparable<?> ,V> ImmutableSortedMap .Builder <K ,V> naturalOrder()
Ordering.natural() as the comparator.
public static <K,V> ImmutableSortedMap .Builder <K ,V> orderedBy(Comparator <K> comparator)
SortedMap<Integer, String> with a
Comparator<Number>, use the
ImmutableSortedMap.Builder constructor instead.
NullPointerException - if
comparator is null
public static <K extends Comparable<?> ,V> ImmutableSortedMap .Builder <K ,V> reverseOrder()
public int size()
public ImmutableSet<Map .Entry <K ,V>> entrySet()
public ImmutableSortedSet<K> keySet()
public ImmutableCollection<V> values()
public Comparator<? super K> comparator()
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.
public K firstKey()
public K lastKey()
public ImmutableSortedMap<K ,V> headMap(K toKey)
ImmutableSortedMap, consisting of the entries whose keys are less than
toKey.
The SortedMap 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.
public ImmutableSortedMap<K ,V> headMap(K toKey, boolean inclusive)
ImmutableSortedMap, consisting of the entries whose keys are less than (or equal to, if
inclusive)
toKey.
The SortedMap 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.
headMap in interface
NavigableMap<K,V>
public ImmutableSortedMap<K ,V> subMap(K fromKey, K toKey)
ImmutableSortedMap, consisting of the entries whose keys ranges from
fromKey, inclusive, to
toKey, exclusive.
The SortedMap 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.
public ImmutableSortedMap<K ,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)
ImmutableSortedMap, consisting of the entries whose keys ranges from
fromKey to
toKey, inclusive or exclusive as indicated by the boolean flags.
The SortedMap 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.
subMap in interface
NavigableMap<K,V>
public ImmutableSortedMap<K ,V> tailMap(K fromKey)
ImmutableSortedMap, consisting of the entries whose keys are greater than or equals to
fromKey.
The SortedMap 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.
public ImmutableSortedMap<K ,V> tailMap(K fromKey, boolean inclusive)
ImmutableSortedMap, consisting of the entries whose keys are greater than (or equal to, if
inclusive)
fromKey.
The SortedMap 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.
tailMap in interface
NavigableMap<K,V>
@Deprecated public final Map.Entry <K ,V> pollFirstEntry()
pollFirstEntry in interface
NavigableMap<K,V>
UnsupportedOperationException - always
@Deprecated public final Map.Entry <K ,V> pollLastEntry()
pollLastEntry in interface
NavigableMap<K,V>
UnsupportedOperationException - always
public ImmutableSortedMap<K ,V> descendingMap()
public ImmutableSortedSet<K> navigableKeySet()
public ImmutableSortedSet<K> descendingKeySet()
@Deprecated public static <K,V> ImmutableSortedMap .Builder <K ,V> builder()
naturalOrder(), which offers better type-safety.
naturalOrder(), which offers better type-safety, instead. This method exists only to hide
ImmutableMap.builder() from consumers of
ImmutableSortedMap.
UnsupportedOperationException - always