Class ImmutableSortedMap<K,V>
- java.lang.Object
-
- com.google.common.collect.ImmutableMap<K,V>
-
- com.google.common.collect.ImmutableSortedMap<K,V>
-
- All Implemented Interfaces:
Serializable
,Map<K,V>
,NavigableMap<K,V>
,SortedMap<K,V>
@GwtCompatible(serializable=true, emulated=true) public abstract class ImmutableSortedMap<K,V> extends ImmutableMap<K,V> implements NavigableMap<K,V>
An immutableSortedMap
. Does not permit null keys or values.Unlike
Collections.unmodifiableSortedMap(java.util.SortedMap<K, ? extends V>)
, which is a view of a separate map which can still change, an instance ofImmutableSortedMap
contains its own data and will never change.ImmutableSortedMap
is convenient forpublic static final
maps ("constant maps") and also lets you easily make a "defensive copy" of a map provided to your class by a caller.Note: Although this class is not final, it cannot be subclassed as it has no public or protected constructors. Thus, instances of this class are guaranteed to be immutable.
See the Guava User Guide article on immutable collections.
- Since:
- 2.0 (imported from Google Collections Library; implements
NavigableMap
since 12.0) - See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ImmutableSortedMap.Builder<K,V>
A builder for creating immutable sorted map instances, especiallypublic static final
maps ("constant maps").
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <K,V>
ImmutableSortedMap.Builder<K,V>builder()
Deprecated.UsenaturalOrder()
, which offers better type-safety.Map.Entry<K,V>
ceilingEntry(K key)
K
ceilingKey(K key)
Comparator<? super K>
comparator()
Returns the comparator that orders the keys, which isOrdering.natural()
when the natural ordering of the keys is used.boolean
containsValue(Object value)
static <K,V>
ImmutableSortedMap<K,V>copyOf(Map<? extends K,? extends V> map)
Returns an immutable map containing the same entries asmap
, sorted by the natural ordering of the keys.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 asmap
, with keys sorted by the provided comparator.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.ImmutableSortedSet<K>
descendingKeySet()
ImmutableSortedMap<K,V>
descendingMap()
ImmutableSet<Map.Entry<K,V>>
entrySet()
Returns an immutable set of the mappings in this map, sorted by the key ordering.Map.Entry<K,V>
firstEntry()
K
firstKey()
Map.Entry<K,V>
floorEntry(K key)
K
floorKey(K key)
ImmutableSortedMap<K,V>
headMap(K toKey)
This method returns aImmutableSortedMap
, consisting of the entries whose keys are less thantoKey
.abstract ImmutableSortedMap<K,V>
headMap(K toKey, boolean inclusive)
This method returns aImmutableSortedMap
, consisting of the entries whose keys are less than (or equal to, ifinclusive
)toKey
.Map.Entry<K,V>
higherEntry(K key)
K
higherKey(K key)
abstract ImmutableSortedSet<K>
keySet()
Returns an immutable sorted set of the keys in this map.Map.Entry<K,V>
lastEntry()
K
lastKey()
Map.Entry<K,V>
lowerEntry(K key)
K
lowerKey(K key)
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.ImmutableSortedSet<K>
navigableKeySet()
static <K,V>
ImmutableSortedMap<K,V>of()
Returns the empty sorted map.static <K extends Comparable<? super K>,V>
ImmutableSortedMap<K,V>of(K k1, V v1)
Returns an immutable map containing a single entry.static <K,V>
ImmutableSortedMap<K,V>of(K k1, V v1)
Deprecated.Pass a key of typeComparable
to useof(Comparable, Object)
.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.static <K,V>
ImmutableSortedMap<K,V>of(K k1, V v1, K k2, V v2)
Deprecated.Pass keys of typeComparable
to useof(Comparable, Object, Comparable, Object)
.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.static <K,V>
ImmutableSortedMap<K,V>of(K k1, V v1, K k2, V v2, K k3, V v3)
Deprecated.Pass keys of typeComparable
to useof(Comparable, Object, Comparable, Object, Comparable, Object)
.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.static <K,V>
ImmutableSortedMap<K,V>of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
Deprecated.Pass keys of typeComparable
to useof(Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object)
.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.static <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)
Deprecated.Pass keys of typeComparable
to useof(Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object)
.static <K,V>
ImmutableSortedMap.Builder<K,V>orderedBy(Comparator<K> comparator)
Returns a builder that creates immutable sorted maps with an explicit comparator.Map.Entry<K,V>
pollFirstEntry()
Deprecated.Unsupported operation.Map.Entry<K,V>
pollLastEntry()
Deprecated.Unsupported operation.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.int
size()
ImmutableSortedMap<K,V>
subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)
This method returns aImmutableSortedMap
, consisting of the entries whose keys ranges fromfromKey
totoKey
, inclusive or exclusive as indicated by the boolean flags.ImmutableSortedMap<K,V>
subMap(K fromKey, K toKey)
This method returns aImmutableSortedMap
, consisting of the entries whose keys ranges fromfromKey
, inclusive, totoKey
, exclusive.ImmutableSortedMap<K,V>
tailMap(K fromKey)
This method returns aImmutableSortedMap
, consisting of the entries whose keys are greater than or equals tofromKey
.abstract ImmutableSortedMap<K,V>
tailMap(K fromKey, boolean inclusive)
This method returns aImmutableSortedMap
, consisting of the entries whose keys are greater than (or equal to, ifinclusive
)fromKey
.abstract ImmutableCollection<V>
values()
Returns an immutable collection of the values in this map, sorted by the ordering of the corresponding keys.-
Methods inherited from class com.google.common.collect.ImmutableMap
asMultimap, clear, containsKey, equals, get, hashCode, isEmpty, put, putAll, remove, toString
-
Methods inherited from interface java.util.Map
clear, compute, computeIfAbsent, computeIfPresent, containsKey, equals, forEach, get, getOrDefault, hashCode, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll
-
-
-
-
Method Detail
-
of
public static <K,V> ImmutableSortedMap<K,V> of()
Returns the empty sorted map.
-
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
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
-
copyOf
public static <K,V> ImmutableSortedMap<K,V> copyOf(Map<? extends K,? extends V> map)
Returns an immutable map containing the same entries asmap
, 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 inmap
are not mutually comparableNullPointerException
- if any key or value inmap
is nullIllegalArgumentException
- 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 asmap
, 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 inmap
is nullIllegalArgumentException
- if any two keys are equal according to the comparator
-
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 inmap
is null
-
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 useOrdering.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 aSortedMap<Integer, String>
with aComparator<Number>
, use theImmutableSortedMap.Builder
constructor instead.- Throws:
NullPointerException
- ifcomparator
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.
-
containsValue
public boolean containsValue(@Nullable Object value)
- Specified by:
containsValue
in interfaceMap<K,V>
- Overrides:
containsValue
in classImmutableMap<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.
-
keySet
public abstract ImmutableSortedSet<K> keySet()
Returns an immutable sorted set of the keys in this map.
-
values
public abstract ImmutableCollection<V> values()
Returns an immutable collection of the values in this map, sorted by the ordering of the corresponding keys.
-
comparator
public Comparator<? super K> comparator()
Returns the comparator that orders the keys, which isOrdering.natural()
when the natural ordering of the keys is used. Note that its behavior is not consistent withTreeMap.comparator()
, which returnsnull
to indicate natural ordering.- Specified by:
comparator
in interfaceSortedMap<K,V>
-
headMap
public ImmutableSortedMap<K,V> headMap(K toKey)
This method returns aImmutableSortedMap
, consisting of the entries whose keys are less thantoKey
.The
SortedMap.headMap(K)
documentation states that a submap of a submap throws anIllegalArgumentException
if passed atoKey
greater than an earliertoKey
. However, this method doesn't throw an exception in that situation, but instead keeps the originaltoKey
.
-
headMap
public abstract ImmutableSortedMap<K,V> headMap(K toKey, boolean inclusive)
This method returns aImmutableSortedMap
, consisting of the entries whose keys are less than (or equal to, ifinclusive
)toKey
.The
SortedMap.headMap(K)
documentation states that a submap of a submap throws anIllegalArgumentException
if passed atoKey
greater than an earliertoKey
. However, this method doesn't throw an exception in that situation, but instead keeps the originaltoKey
.- Specified by:
headMap
in interfaceNavigableMap<K,V>
- Since:
- 12.0
-
subMap
public ImmutableSortedMap<K,V> subMap(K fromKey, K toKey)
This method returns aImmutableSortedMap
, consisting of the entries whose keys ranges fromfromKey
, inclusive, totoKey
, exclusive.The
SortedMap.subMap(K, K)
documentation states that a submap of a submap throws anIllegalArgumentException
if passed afromKey
less than an earlierfromKey
. However, this method doesn't throw an exception in that situation, but instead keeps the originalfromKey
. Similarly, this method keeps the originaltoKey
, instead of throwing an exception, if passed atoKey
greater than an earliertoKey
.
-
subMap
public ImmutableSortedMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)
This method returns aImmutableSortedMap
, consisting of the entries whose keys ranges fromfromKey
totoKey
, inclusive or exclusive as indicated by the boolean flags.The
SortedMap.subMap(K, K)
documentation states that a submap of a submap throws anIllegalArgumentException
if passed afromKey
less than an earlierfromKey
. However, this method doesn't throw an exception in that situation, but instead keeps the originalfromKey
. Similarly, this method keeps the originaltoKey
, instead of throwing an exception, if passed atoKey
greater than an earliertoKey
.- Specified by:
subMap
in interfaceNavigableMap<K,V>
- Since:
- 12.0
-
tailMap
public ImmutableSortedMap<K,V> tailMap(K fromKey)
This method returns aImmutableSortedMap
, consisting of the entries whose keys are greater than or equals tofromKey
.The
SortedMap.tailMap(K)
documentation states that a submap of a submap throws anIllegalArgumentException
if passed afromKey
less than an earlierfromKey
. However, this method doesn't throw an exception in that situation, but instead keeps the originalfromKey
.
-
tailMap
public abstract ImmutableSortedMap<K,V> tailMap(K fromKey, boolean inclusive)
This method returns aImmutableSortedMap
, consisting of the entries whose keys are greater than (or equal to, ifinclusive
)fromKey
.The
SortedMap.tailMap(K)
documentation states that a submap of a submap throws anIllegalArgumentException
if passed afromKey
less than an earlierfromKey
. However, this method doesn't throw an exception in that situation, but instead keeps the originalfromKey
.- Specified by:
tailMap
in interfaceNavigableMap<K,V>
- Since:
- 12.0
-
lowerEntry
public Map.Entry<K,V> lowerEntry(K key)
- Specified by:
lowerEntry
in interfaceNavigableMap<K,V>
-
floorEntry
public Map.Entry<K,V> floorEntry(K key)
- Specified by:
floorEntry
in interfaceNavigableMap<K,V>
-
ceilingEntry
public Map.Entry<K,V> ceilingEntry(K key)
- Specified by:
ceilingEntry
in interfaceNavigableMap<K,V>
-
ceilingKey
public K ceilingKey(K key)
- Specified by:
ceilingKey
in interfaceNavigableMap<K,V>
-
higherEntry
public Map.Entry<K,V> higherEntry(K key)
- Specified by:
higherEntry
in interfaceNavigableMap<K,V>
-
firstEntry
public Map.Entry<K,V> firstEntry()
- Specified by:
firstEntry
in interfaceNavigableMap<K,V>
-
lastEntry
public Map.Entry<K,V> lastEntry()
- Specified by:
lastEntry
in interfaceNavigableMap<K,V>
-
pollFirstEntry
@Deprecated public final Map.Entry<K,V> pollFirstEntry()
Deprecated.Unsupported operation.Guaranteed to throw an exception and leave the map unmodified.- Specified by:
pollFirstEntry
in interfaceNavigableMap<K,V>
- Throws:
UnsupportedOperationException
- always
-
pollLastEntry
@Deprecated public final Map.Entry<K,V> pollLastEntry()
Deprecated.Unsupported operation.Guaranteed to throw an exception and leave the map unmodified.- Specified by:
pollLastEntry
in interfaceNavigableMap<K,V>
- Throws:
UnsupportedOperationException
- always
-
descendingMap
public ImmutableSortedMap<K,V> descendingMap()
- Specified by:
descendingMap
in interfaceNavigableMap<K,V>
-
navigableKeySet
public ImmutableSortedSet<K> navigableKeySet()
- Specified by:
navigableKeySet
in interfaceNavigableMap<K,V>
-
descendingKeySet
public ImmutableSortedSet<K> descendingKeySet()
- Specified by:
descendingKeySet
in interfaceNavigableMap<K,V>
-
builder
@Deprecated public static <K,V> ImmutableSortedMap.Builder<K,V> builder()
Deprecated.UsenaturalOrder()
, which offers better type-safety.Not supported. UsenaturalOrder()
, which offers better type-safety, instead. This method exists only to hideImmutableMap.builder()
from consumers ofImmutableSortedMap
.- Throws:
UnsupportedOperationException
- always
-
of
@Deprecated public static <K,V> ImmutableSortedMap<K,V> of(K k1, V v1)
Deprecated.Pass a key of typeComparable
to useof(Comparable, Object)
.Not supported. You are attempting to create a map that may contain a non-Comparable
key. Proper calls will resolve to the version inImmutableSortedMap
, not this dummy version.- Throws:
UnsupportedOperationException
- always
-
of
@Deprecated public static <K,V> ImmutableSortedMap<K,V> of(K k1, V v1, K k2, V v2)
Deprecated.Pass keys of typeComparable
to useof(Comparable, Object, Comparable, Object)
.Not supported. You are attempting to create a map that may contain non-Comparable
keys. Proper calls will resolve to the version inImmutableSortedMap
, not this dummy version.- Throws:
UnsupportedOperationException
- always
-
of
@Deprecated public static <K,V> ImmutableSortedMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3)
Deprecated.Pass keys of typeComparable
to useof(Comparable, Object, Comparable, Object, Comparable, Object)
.Not supported. You are attempting to create a map that may contain non-Comparable
keys. Proper calls to will resolve to the version inImmutableSortedMap
, not this dummy version.- Throws:
UnsupportedOperationException
- always
-
of
@Deprecated public static <K,V> ImmutableSortedMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
Deprecated.Pass keys of typeComparable
to useof(Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object)
.Not supported. You are attempting to create a map that may contain non-Comparable
keys. Proper calls will resolve to the version inImmutableSortedMap
, not this dummy version.- Throws:
UnsupportedOperationException
- always
-
of
@Deprecated public static <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)
Deprecated.Pass keys of typeComparable
to useof(Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object)
.Not supported. You are attempting to create a map that may contain non-Comparable
keys. Proper calls will resolve to the version inImmutableSortedMap
, not this dummy version.- Throws:
UnsupportedOperationException
- always
-
-