@GwtCompatible(serializable=true, emulated=true) public abstract class ImmutableSortedMap<K,V> extends ImmutableMap<K,V> implements NavigableMap<K,V>
SortedMap
. 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 of ImmutableSortedMap
contains its own data and will never change.
ImmutableSortedMap
is convenient for public 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.
NavigableMap
since 12.0)Modifier and Type | Class and Description |
---|---|
static class |
ImmutableSortedMap.Builder<K,V>
A builder for creating immutable sorted map instances, especially
public static final maps ("constant maps"). |
Modifier and Type | Method and Description |
---|---|
static <K,V> ImmutableSortedMap.Builder<K,V> |
builder()
Deprecated.
Use
naturalOrder() , which offers
better type-safety. |
Map.Entry<K,V> |
ceilingEntry(K key)
Returns a key-value mapping associated with the least key
greater than or equal to the given key, or
null if
there is no such key. |
K |
ceilingKey(K key)
Returns the least key greater than or equal to the given key,
or
null if there is no such key. |
Comparator<? super K> |
comparator()
Returns the comparator that orders the keys, which is
Ordering.natural() when the natural ordering of the keys is used. |
boolean |
containsValue(Object value)
Returns true if this map maps one or more keys to the
specified value.
|
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. |
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. |
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()
Returns a reverse order
NavigableSet view of the keys contained in this map. |
ImmutableSortedMap<K,V> |
descendingMap()
Returns a reverse order view of the mappings contained in this map.
|
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()
Returns a key-value mapping associated with the least
key in this map, or
null if the map is empty. |
K |
firstKey()
Returns the first (lowest) key currently in this map.
|
Map.Entry<K,V> |
floorEntry(K key)
Returns a key-value mapping associated with the greatest key
less than or equal to the given key, or
null if there
is no such key. |
K |
floorKey(K key)
Returns the greatest key less than or equal to the given key,
or
null if there is no such key. |
ImmutableSortedMap<K,V> |
headMap(K toKey)
This method returns a
ImmutableSortedMap , consisting of the entries
whose keys are less than toKey . |
abstract 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 . |
Map.Entry<K,V> |
higherEntry(K key)
Returns a key-value mapping associated with the least key
strictly greater than the given key, or
null if there
is no such key. |
K |
higherKey(K key)
Returns the least key strictly greater than the given key, or
null if there is no such key. |
abstract ImmutableSortedSet<K> |
keySet()
Returns an immutable sorted set of the keys in this map.
|
Map.Entry<K,V> |
lastEntry()
Returns a key-value mapping associated with the greatest
key in this map, or
null if the map is empty. |
K |
lastKey()
Returns the last (highest) key currently in this map.
|
Map.Entry<K,V> |
lowerEntry(K key)
Returns a key-value mapping associated with the greatest key
strictly less than the given key, or
null if there is
no such key. |
K |
lowerKey(K key)
Returns the greatest key strictly less than the given key, or
null if there is no such key. |
static <K extends Comparable<?>,V> |
naturalOrder()
Returns a builder that creates immutable sorted maps whose keys are
ordered by their natural ordering.
|
ImmutableSortedSet<K> |
navigableKeySet()
Returns a
NavigableSet view of the keys contained in this map. |
static <K,V> ImmutableSortedMap<K,V> |
of()
Returns the empty sorted map.
|
static <K extends Comparable<? super K>,V> |
of(K k1,
V v1)
Returns an immutable map containing a single entry.
|
static <K extends Comparable<? super 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 Comparable<? super 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 Comparable<? super 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 Comparable<? super 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.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> |
reverseOrder()
Returns a builder that creates immutable sorted maps whose keys are
ordered by the reverse of their natural ordering.
|
int |
size()
Returns the number of key-value mappings in this map.
|
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. |
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. |
ImmutableSortedMap<K,V> |
tailMap(K fromKey)
This method returns a
ImmutableSortedMap , consisting of the entries
whose keys are greater than or equals to fromKey . |
abstract 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 . |
abstract ImmutableCollection<V> |
values()
Returns an immutable collection of the values in this map, sorted by the
ordering of the corresponding keys.
|
asMultimap, clear, containsKey, equals, get, hashCode, isEmpty, put, putAll, remove, toString
public 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 orderingpublic 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 orderingpublic 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 orderingpublic 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 orderingpublic 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
comparableNullPointerException
- if any key or value in map
is nullIllegalArgumentException
- if any two keys are equal according to
their natural orderingpublic 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 nullIllegalArgumentException
- if any two keys are equal according to the
comparatorpublic 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 nullpublic 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 nullpublic static <K extends Comparable<?>,V> ImmutableSortedMap.Builder<K,V> reverseOrder()
public int size()
java.util.Map
public boolean containsValue(@Nullable Object value)
java.util.Map
containsValue
in interface Map<K,V>
containsValue
in class ImmutableMap<K,V>
value
- value whose presence in this map is to be testedpublic ImmutableSet<Map.Entry<K,V>> entrySet()
public abstract ImmutableSortedSet<K> keySet()
public abstract 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.comparator
in interface SortedMap<K,V>
null
if this map uses the natural ordering
of its keyspublic K firstKey()
java.util.SortedMap
public K lastKey()
java.util.SortedMap
public ImmutableSortedMap<K,V> headMap(K toKey)
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
.
public abstract 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.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
.
headMap
in interface NavigableMap<K,V>
toKey
- high endpoint of the keys in the returned mapinclusive
- true
if the high endpoint
is to be included in the returned viewinclusive
is true) toKey
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.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
.
subMap
in interface NavigableMap<K,V>
subMap
in interface SortedMap<K,V>
fromKey
- low endpoint (inclusive) of the keys in the returned maptoKey
- high endpoint (exclusive) of the keys in the returned mapfromKey
, inclusive, to toKey
, exclusivepublic 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.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
.
subMap
in interface NavigableMap<K,V>
fromKey
- low endpoint of the keys in the returned mapfromInclusive
- true
if the low endpoint
is to be included in the returned viewtoKey
- high endpoint of the keys in the returned maptoInclusive
- true
if the high endpoint
is to be included in the returned viewfromKey
to toKey
public ImmutableSortedMap<K,V> tailMap(K fromKey)
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
.
public abstract 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.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
.
tailMap
in interface NavigableMap<K,V>
fromKey
- low endpoint of the keys in the returned mapinclusive
- true
if the low endpoint
is to be included in the returned viewinclusive
is true) fromKey
public Map.Entry<K,V> lowerEntry(K key)
java.util.NavigableMap
null
if there is
no such key.lowerEntry
in interface NavigableMap<K,V>
key
- the keykey
,
or null
if there is no such keypublic K lowerKey(K key)
java.util.NavigableMap
null
if there is no such key.lowerKey
in interface NavigableMap<K,V>
key
- the keykey
,
or null
if there is no such keypublic Map.Entry<K,V> floorEntry(K key)
java.util.NavigableMap
null
if there
is no such key.floorEntry
in interface NavigableMap<K,V>
key
- the keykey
, or null
if there is no such keypublic K floorKey(K key)
java.util.NavigableMap
null
if there is no such key.floorKey
in interface NavigableMap<K,V>
key
- the keykey
,
or null
if there is no such keypublic Map.Entry<K,V> ceilingEntry(K key)
java.util.NavigableMap
null
if
there is no such key.ceilingEntry
in interface NavigableMap<K,V>
key
- the keykey
, or null
if there is no such keypublic K ceilingKey(K key)
java.util.NavigableMap
null
if there is no such key.ceilingKey
in interface NavigableMap<K,V>
key
- the keykey
,
or null
if there is no such keypublic Map.Entry<K,V> higherEntry(K key)
java.util.NavigableMap
null
if there
is no such key.higherEntry
in interface NavigableMap<K,V>
key
- the keykey
,
or null
if there is no such keypublic K higherKey(K key)
java.util.NavigableMap
null
if there is no such key.higherKey
in interface NavigableMap<K,V>
key
- the keykey
,
or null
if there is no such keypublic Map.Entry<K,V> firstEntry()
java.util.NavigableMap
null
if the map is empty.firstEntry
in interface NavigableMap<K,V>
null
if this map is emptypublic Map.Entry<K,V> lastEntry()
java.util.NavigableMap
null
if the map is empty.lastEntry
in interface NavigableMap<K,V>
null
if this map is empty@Deprecated public final Map.Entry<K,V> pollFirstEntry()
pollFirstEntry
in interface NavigableMap<K,V>
null
if this map is emptyUnsupportedOperationException
- always@Deprecated public final Map.Entry<K,V> pollLastEntry()
pollLastEntry
in interface NavigableMap<K,V>
null
if this map is emptyUnsupportedOperationException
- alwayspublic ImmutableSortedMap<K,V> descendingMap()
java.util.NavigableMap
remove
operation), the results of the iteration are undefined.
The returned map has an ordering equivalent to
Collections.reverseOrder
(comparator()).
The expression m.descendingMap().descendingMap()
returns a
view of m
essentially equivalent to m
.
descendingMap
in interface NavigableMap<K,V>
public ImmutableSortedSet<K> navigableKeySet()
java.util.NavigableMap
NavigableSet
view of the keys contained in this map.
The set's iterator returns the keys in ascending order.
The set is backed by the map, so changes to the map are reflected in
the set, and vice-versa. If the map is modified while an iteration
over the set is in progress (except through the iterator's own remove
operation), the results of the iteration are undefined. The
set supports element removal, which removes the corresponding mapping
from the map, via the Iterator.remove
, Set.remove
,
removeAll
, retainAll
, and clear
operations.
It does not support the add
or addAll
operations.navigableKeySet
in interface NavigableMap<K,V>
public ImmutableSortedSet<K> descendingKeySet()
java.util.NavigableMap
NavigableSet
view of the keys contained in this map.
The set's iterator returns the keys in descending order.
The set is backed by the map, so changes to the map are reflected in
the set, and vice-versa. If the map is modified while an iteration
over the set is in progress (except through the iterator's own remove
operation), the results of the iteration are undefined. The
set supports element removal, which removes the corresponding mapping
from the map, via the Iterator.remove
, Set.remove
,
removeAll
, retainAll
, and clear
operations.
It does not support the add
or addAll
operations.descendingKeySet
in interface NavigableMap<K,V>
@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
- alwaysCopyright © 2010-2014. All Rights Reserved.