Class ImmutableSortedMap<K,V>
- java.lang.Object
-
- com.google.common.collect.ImmutableMap<K,V>
-
- com.google.common.collect.ImmutableSortedMapFauxverideShim<K,V>
-
- com.google.common.collect.ImmutableSortedMap<K,V>
-
- All Implemented Interfaces:
java.io.Serializable
,java.util.Map<K,V>
,java.util.NavigableMap<K,V>
,java.util.SortedMap<K,V>
@GwtCompatible(serializable=true, emulated=true) public final class ImmutableSortedMap<K,V> extends ImmutableSortedMapFauxverideShim<K,V> implements java.util.NavigableMap<K,V>
ANavigableMap
whose contents will never change, with many other important properties detailed atImmutableCollection
.Warning: as with any sorted collection, you are strongly advised not to use a
Comparator
orComparable
type whose comparison behavior is inconsistent with equals. That is,a.compareTo(b)
orcomparator.compare(a, b)
should equal zero if and only ifa.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:
- 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").private static class
ImmutableSortedMap.SerializedForm
Serialized type for all ImmutableSortedMap instances.-
Nested classes/interfaces inherited from class com.google.common.collect.ImmutableMap
ImmutableMap.IteratorBasedImmutableMap<K,V>
-
-
Field Summary
Fields Modifier and Type Field Description private ImmutableSortedMap<K,V>
descendingMap
private RegularImmutableSortedSet<K>
keySet
private static ImmutableSortedMap<java.lang.Comparable,java.lang.Object>
NATURAL_EMPTY_MAP
private static java.util.Comparator<java.lang.Comparable>
NATURAL_ORDER
private static long
serialVersionUID
private ImmutableList<V>
valueList
-
Fields inherited from class com.google.common.collect.ImmutableMap
EMPTY_ENTRY_ARRAY
-
-
Constructor Summary
Constructors Constructor Description ImmutableSortedMap(RegularImmutableSortedSet<K> keySet, ImmutableList<V> valueList)
ImmutableSortedMap(RegularImmutableSortedSet<K> keySet, ImmutableList<V> valueList, ImmutableSortedMap<K,V> descendingMap)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.util.Map.Entry<K,V>
ceilingEntry(K key)
K
ceilingKey(K key)
java.util.Comparator<? super K>
comparator()
Returns the comparator that orders the keys, which isOrdering.natural()
when the natural ordering of the keys is used.static <K,V>
ImmutableSortedMap<K,V>copyOf(java.lang.Iterable<? extends java.util.Map.Entry<? extends K,? extends V>> entries)
Returns an immutable map containing the given entries, with keys sorted by the provided comparator.static <K,V>
ImmutableSortedMap<K,V>copyOf(java.lang.Iterable<? extends java.util.Map.Entry<? extends K,? extends V>> entries, java.util.Comparator<? super K> comparator)
Returns an immutable map containing the given entries, with keys sorted by the provided comparator.static <K,V>
ImmutableSortedMap<K,V>copyOf(java.util.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(java.util.Map<? extends K,? extends V> map, java.util.Comparator<? super K> comparator)
Returns an immutable map containing the same entries asmap
, with keys sorted by the provided comparator.private static <K,V>
ImmutableSortedMap<K,V>copyOfInternal(java.util.Map<? extends K,? extends V> map, java.util.Comparator<? super K> comparator)
static <K,V>
ImmutableSortedMap<K,V>copyOfSorted(java.util.SortedMap<K,? extends V> map)
Returns an immutable map containing the same entries as the provided sorted map, with the same ordering.(package private) ImmutableSet<java.util.Map.Entry<K,V>>
createEntrySet()
ImmutableSortedSet<K>
descendingKeySet()
ImmutableSortedMap<K,V>
descendingMap()
(package private) static <K,V>
ImmutableSortedMap<K,V>emptyMap(java.util.Comparator<? super K> comparator)
ImmutableSet<java.util.Map.Entry<K,V>>
entrySet()
Returns an immutable set of the mappings in this map, sorted by the key ordering.java.util.Map.Entry<K,V>
firstEntry()
K
firstKey()
java.util.Map.Entry<K,V>
floorEntry(K key)
K
floorKey(K key)
private static <K,V>
ImmutableSortedMap<K,V>fromEntries(java.util.Comparator<? super K> comparator, boolean sameComparator, java.lang.Iterable<? extends java.util.Map.Entry<? extends K,? extends V>> entries)
Accepts a collection of possibly-null entries.private static <K,V>
ImmutableSortedMap<K,V>fromEntries(java.util.Comparator<? super K> comparator, boolean sameComparator, java.util.Map.Entry<K,V>[] entryArray, int size)
V
get(java.lang.Object key)
private ImmutableSortedMap<K,V>
getSubMap(int fromIndex, int toIndex)
ImmutableSortedMap<K,V>
headMap(K toKey)
This method returns aImmutableSortedMap
, consisting of the entries whose keys are less thantoKey
.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
.java.util.Map.Entry<K,V>
higherEntry(K key)
K
higherKey(K key)
(package private) boolean
isPartialView()
ImmutableSortedSet<K>
keySet()
Returns an immutable sorted set of the keys in this map.java.util.Map.Entry<K,V>
lastEntry()
K
lastKey()
java.util.Map.Entry<K,V>
lowerEntry(K key)
K
lowerKey(K key)
static <K extends java.lang.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.private static <K,V>
ImmutableSortedMap<K,V>of(java.util.Comparator<? super K> comparator, K k1, V v1)
Returns an immutable map containing a single entry.static <K extends java.lang.Comparable<? super K>,V>
ImmutableSortedMap<K,V>of(K k1, V v1)
Returns an immutable map containing a single entry.static <K extends java.lang.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 extends java.lang.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 extends java.lang.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 extends java.lang.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.private static <K extends java.lang.Comparable<? super K>,V>
ImmutableSortedMap<K,V>ofEntries(ImmutableMapEntry<K,V>... entries)
static <K,V>
ImmutableSortedMap.Builder<K,V>orderedBy(java.util.Comparator<K> comparator)
Returns a builder that creates immutable sorted maps with an explicit comparator.java.util.Map.Entry<K,V>
pollFirstEntry()
Deprecated.Unsupported operation.java.util.Map.Entry<K,V>
pollLastEntry()
Deprecated.Unsupported operation.static <K extends java.lang.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
.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
.ImmutableCollection<V>
values()
Returns an immutable collection of the values in this map, sorted by the ordering of the corresponding keys.(package private) java.lang.Object
writeReplace()
-
Methods inherited from class com.google.common.collect.ImmutableSortedMapFauxverideShim
builder, of, of, of, of, of
-
Methods inherited from class com.google.common.collect.ImmutableMap
asMultimap, checkNoConflict, clear, containsKey, containsValue, createKeySet, createValues, entryOf, equals, hashCode, isEmpty, isHashCodeFast, keyIterator, put, putAll, remove, toString
-
-
-
-
Field Detail
-
NATURAL_ORDER
private static final java.util.Comparator<java.lang.Comparable> NATURAL_ORDER
-
NATURAL_EMPTY_MAP
private static final ImmutableSortedMap<java.lang.Comparable,java.lang.Object> NATURAL_EMPTY_MAP
-
keySet
private final transient RegularImmutableSortedSet<K> keySet
-
valueList
private final transient ImmutableList<V> valueList
-
descendingMap
private transient ImmutableSortedMap<K,V> descendingMap
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ImmutableSortedMap
ImmutableSortedMap(RegularImmutableSortedSet<K> keySet, ImmutableList<V> valueList)
-
ImmutableSortedMap
ImmutableSortedMap(RegularImmutableSortedSet<K> keySet, ImmutableList<V> valueList, ImmutableSortedMap<K,V> descendingMap)
-
-
Method Detail
-
emptyMap
static <K,V> ImmutableSortedMap<K,V> emptyMap(java.util.Comparator<? super K> comparator)
-
of
public static <K,V> ImmutableSortedMap<K,V> of()
Returns the empty sorted map.
-
of
public static <K extends java.lang.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(java.util.Comparator<? super K> comparator, K k1, V v1)
Returns an immutable map containing a single entry.
-
ofEntries
private static <K extends java.lang.Comparable<? super K>,V> ImmutableSortedMap<K,V> ofEntries(ImmutableMapEntry<K,V>... entries)
-
of
public static <K extends java.lang.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:
java.lang.IllegalArgumentException
- if the two keys are equal according to their natural ordering
-
of
public static <K extends java.lang.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:
java.lang.IllegalArgumentException
- if any two keys are equal according to their natural ordering
-
of
public static <K extends java.lang.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:
java.lang.IllegalArgumentException
- if any two keys are equal according to their natural ordering
-
of
public static <K extends java.lang.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:
java.lang.IllegalArgumentException
- if any two keys are equal according to their natural ordering
-
copyOf
public static <K,V> ImmutableSortedMap<K,V> copyOf(java.util.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:
java.lang.ClassCastException
- if the keys inmap
are not mutually comparablejava.lang.NullPointerException
- if any key or value inmap
is nulljava.lang.IllegalArgumentException
- if any two keys are equal according to their natural ordering
-
copyOf
public static <K,V> ImmutableSortedMap<K,V> copyOf(java.util.Map<? extends K,? extends V> map, java.util.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:
java.lang.NullPointerException
- if any key or value inmap
is nulljava.lang.IllegalArgumentException
- if any two keys are equal according to the comparator
-
copyOf
@Beta public static <K,V> ImmutableSortedMap<K,V> copyOf(java.lang.Iterable<? extends java.util.Map.Entry<? extends K,? extends V>> entries)
Returns an immutable map containing the given entries, with keys sorted by the provided comparator.This method is not type-safe, as it may be called on a map with keys that are not mutually comparable.
- Throws:
java.lang.NullPointerException
- if any key or value inmap
is nulljava.lang.IllegalArgumentException
- if any two keys are equal according to the comparator- Since:
- 19.0
-
copyOf
@Beta public static <K,V> ImmutableSortedMap<K,V> copyOf(java.lang.Iterable<? extends java.util.Map.Entry<? extends K,? extends V>> entries, java.util.Comparator<? super K> comparator)
Returns an immutable map containing the given entries, with keys sorted by the provided comparator.- Throws:
java.lang.NullPointerException
- if any key or value inmap
is nulljava.lang.IllegalArgumentException
- if any two keys are equal according to the comparator- Since:
- 19.0
-
copyOfSorted
public static <K,V> ImmutableSortedMap<K,V> copyOfSorted(java.util.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:
java.lang.NullPointerException
- if any key or value inmap
is null
-
copyOfInternal
private static <K,V> ImmutableSortedMap<K,V> copyOfInternal(java.util.Map<? extends K,? extends V> map, java.util.Comparator<? super K> comparator)
-
fromEntries
private static <K,V> ImmutableSortedMap<K,V> fromEntries(java.util.Comparator<? super K> comparator, boolean sameComparator, java.lang.Iterable<? extends java.util.Map.Entry<? extends K,? extends V>> entries)
Accepts a collection of possibly-null entries. IfsameComparator
, 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(java.util.Comparator<? super K> comparator, boolean sameComparator, java.util.Map.Entry<K,V>[] entryArray, int size)
-
naturalOrder
public static <K extends java.lang.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(java.util.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:
java.lang.NullPointerException
- ifcomparator
is null
-
reverseOrder
public static <K extends java.lang.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.
-
get
public V get(@Nullable java.lang.Object key)
-
isPartialView
boolean isPartialView()
- Specified by:
isPartialView
in classImmutableMap<K,V>
-
entrySet
public ImmutableSet<java.util.Map.Entry<K,V>> entrySet()
Returns an immutable set of the mappings in this map, sorted by the key ordering.
-
createEntrySet
ImmutableSet<java.util.Map.Entry<K,V>> createEntrySet()
- Specified by:
createEntrySet
in classImmutableMap<K,V>
-
keySet
public ImmutableSortedSet<K> keySet()
Returns an immutable sorted set of the keys in this map.
-
values
public ImmutableCollection<V> values()
Returns an immutable collection of the values in this map, sorted by the ordering of the corresponding keys.
-
comparator
public java.util.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.
-
getSubMap
private ImmutableSortedMap<K,V> getSubMap(int fromIndex, int toIndex)
-
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 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
.
-
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
.
-
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 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
.
-
pollFirstEntry
@Deprecated public final java.util.Map.Entry<K,V> pollFirstEntry()
Deprecated.Unsupported operation.Guaranteed to throw an exception and leave the map unmodified.
-
pollLastEntry
@Deprecated public final java.util.Map.Entry<K,V> pollLastEntry()
Deprecated.Unsupported operation.Guaranteed to throw an exception and leave the map unmodified.
-
descendingMap
public ImmutableSortedMap<K,V> descendingMap()
-
navigableKeySet
public ImmutableSortedSet<K> navigableKeySet()
-
descendingKeySet
public ImmutableSortedSet<K> descendingKeySet()
-
writeReplace
java.lang.Object writeReplace()
- Overrides:
writeReplace
in classImmutableMap<K,V>
-
-