java.lang.Object | ||
↳ | java.util.AbstractMap<K, V> | |
↳ | java.util.TreeMap<K, V> |
A map whose entries are sorted by their keys. All optional operations such as put(K, V)
and remove(Object)
are supported.
This map sorts keys using either a user-supplied comparator or the key's natural order:
comparator()
. Comparable
and compareTo()
must be able to compare each key with any other key in this map. In this case comparator()
will return null. a
and
b
,
a.equals(b)
if and only if
compare(a, b) == 0
.
When the ordering is not consistent with equals the behavior of this class is well defined but does not honor the contract specified by Map
. Consider a tree map of case-insensitive strings, an ordering that is not consistent with equals:
TreeMap
map = new TreeMap
(String.CASE_INSENSITIVE_ORDER); map.put("a", "android"); // The Map API specifies that the next line should print "null" because // "a".equals("A") is false and there is no mapping for upper case "A". // But the case insensitive ordering says compare("a", "A") == 0. TreeMap // uses only comparators/comparable on keys and so this prints "android". System.out.println(map.get("A"));
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
Create a natural order, empty tree map whose keys must be mutually comparable and non-null.
|
||||||||||
|
Create a natural order tree map populated with the key/value pairs of
copyFrom .
|
||||||||||
|
Create a tree map ordered by
comparator .
|
||||||||||
|
Create a tree map with the ordering and key/value pairs of
copyFrom .
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
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.
|
||||||||||
|
Returns the least key greater than or equal to the given key, or
null if there is no such key.
|
||||||||||
|
Removes all elements from this
Map , leaving it empty.
This implementation calls |
||||||||||
|
Creates and returns a copy of this
Object .
|
||||||||||
|
Returns the comparator used to compare keys in this sorted map.
|
||||||||||
|
Returns whether this
Map contains the specified key.
This implementation iterates its key set, looking for a key that |
||||||||||
|
Returns a reverse order
NavigableSet view of the keys contained in this map.
|
||||||||||
|
Returns a reverse order view of the mappings contained in this map.
|
||||||||||
|
Returns a
Set containing all of the mappings in this
Map .
|
||||||||||
|
Returns a key-value mapping associated with the least key in this map, or
null if the map is empty.
|
||||||||||
|
Returns the first key in this sorted map.
|
||||||||||
|
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.
|
||||||||||
|
Returns the greatest key less than or equal to the given key, or
null if there is no such key.
|
||||||||||
|
Returns the value of the mapping with the specified key.
This implementation iterates its entry set, looking for an entry with a key that |
||||||||||
|
Returns a view of the portion of this map whose keys are less than (or equal to, if
inclusive is true)
toKey .
|
||||||||||
|
Returns a sorted map over a range of this sorted map with all keys that are less than the specified
endKey .
Equivalent to |
||||||||||
|
Returns a key-value mapping associated with the least key strictly greater than the given key, or
null if there is no such key.
|
||||||||||
|
Returns the least key strictly greater than the given key, or
null if there is no such key.
|
||||||||||
|
Returns whether this map is empty.
This implementation compares |
||||||||||
|
Returns a set of the keys contained in this
Map .
This implementation returns a view that calls through this to map. |
||||||||||
|
Returns a key-value mapping associated with the greatest key in this map, or
null if the map is empty.
|
||||||||||
|
Returns the last key in this sorted map.
|
||||||||||
|
Returns a key-value mapping associated with the greatest key strictly less than the given key, or
null if there is no such key.
|
||||||||||
|
Returns the greatest key strictly less than the given key, or
null if there is no such key.
|
||||||||||
|
Returns a
NavigableSet view of the keys contained in this map.
|
||||||||||
|
Removes and returns a key-value mapping associated with the least key in this map, or
null if the map is empty.
|
||||||||||
|
Removes and returns a key-value mapping associated with the greatest key in this map, or
null if the map is empty.
|
||||||||||
|
Maps the specified key to the specified value.
This base implementation throws |
||||||||||
|
Removes a mapping with the specified key from this
Map .
This implementation iterates its entry set, removing the entry with a key that |
||||||||||
|
Returns the number of mappings in this
Map .
This implementation returns its entry set's size. |
||||||||||
|
Returns a sorted map over a range of this sorted map with all keys greater than or equal to the specified
startKey and less than the specified
endKey .
Equivalent to |
||||||||||
|
Returns a view of the portion of this map whose keys range from
fromKey to
toKey .
|
||||||||||
|
Returns a view of the portion of this map whose keys are greater than (or equal to, if
inclusive is true)
fromKey .
|
||||||||||
|
Returns a sorted map over a range of this sorted map with all keys that are greater than or equal to the specified
startKey .
Equivalent to |
[Expand]
Inherited Methods
|
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
|||||||||||
![]() |
|||||||||||
![]() |
|||||||||||
![]() |
|||||||||||
![]() |
Create a natural order, empty tree map whose keys must be mutually comparable and non-null.
Create a natural order tree map populated with the key/value pairs of copyFrom
. This map's keys must be mutually comparable and non-null.
Even if copyFrom
is a SortedMap
, the constructed map will not use copyFrom
's ordering. This constructor always creates a naturally-ordered map. Because the TreeMap
constructor overloads are ambiguous, prefer to construct a map and populate it in two steps:
TreeMap
customOrderedMap = new TreeMap
(copyFrom.comparator()); customOrderedMap.putAll(copyFrom);
Create a tree map ordered by comparator
. This map's keys may only be null if comparator
permits.
comparator | the comparator to order elements with, or null to use the natural ordering. |
---|
Create a tree map with the ordering and key/value pairs of copyFrom
. This map's keys may only be null if the copyFrom
's ordering permits.
The constructed map will always use copyFrom
's ordering. Because the TreeMap
constructor overloads are ambiguous, prefer to construct a map and populate it in two steps:
TreeMap
customOrderedMap = new TreeMap
(copyFrom.comparator()); customOrderedMap.putAll(copyFrom);
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.
key | the key |
---|
key
, or null
if there is no such keyReturns the least key greater than or equal to the given key, or null
if there is no such key.
key | the key |
---|
key
, or null
if there is no such keyRemoves all elements from this Map
, leaving it empty.
This implementation calls entrySet().clear()
.
Creates and returns a copy of this Object
. The default implementation returns a so-called "shallow" copy: It creates a new instance of the same class and then copies the field values (including object references) from this instance to the new instance. A "deep" copy, in contrast, would also recursively clone nested objects. A subclass that needs to implement this kind of cloning should call super.clone()
to create the new instance and then create deep copies of the nested, mutable objects.
Returns the comparator used to compare keys in this sorted map.
null
if the natural order is used. Returns whether this Map
contains the specified key.
This implementation iterates its key set, looking for a key that key
equals.
key | the key to search for. |
---|
true
if this map contains the specified key, false
otherwise. Returns a reverse order 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.
Returns a reverse order view of the mappings contained in this map. The descending map is backed by this map, so changes to the map are reflected in the descending map, and vice-versa. If either map is modified while an iteration over a collection view of either map is in progress (except through the iterator's own 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
.
Returns a Set
containing all of the mappings in this Map
. Each mapping is an instance of Map.Entry
. As the Set
is backed by this Map
, changes in one will be reflected in the other.
Returns a key-value mapping associated with the least key in this map, or null
if the map is empty.
null
if this map is empty Returns the first key in this sorted map.
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.
key | the key |
---|
key
, or null
if there is no such keyReturns the greatest key less than or equal to the given key, or null
if there is no such key.
key | the key |
---|
key
, or null
if there is no such keyReturns the value of the mapping with the specified key.
This implementation iterates its entry set, looking for an entry with a key that key
equals.
key | the key. |
---|
null
if no mapping for the specified key is found. Returns a view of the portion of this map whose keys are less than (or equal to, if inclusive
is true) toKey
. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.
The returned map will throw an IllegalArgumentException
on an attempt to insert a key outside its range.
to | high endpoint of the keys in the returned map |
---|---|
inclusive | true if the high endpoint is to be included in the returned view |
inclusive
is true) toKey
Returns a sorted map over a range of this sorted map with all keys that are less than the specified endKey
. Changes to the returned sorted map are reflected in this sorted map and vice versa.
Note: The returned map will not allow an insertion of a key outside the specified range.
Equivalent to headMap(toKey, false)
.
toExclusive | the high boundary of the range specified. |
---|
endKey
.Returns a key-value mapping associated with the least key strictly greater than the given key, or null
if there is no such key.
key | the key |
---|
key
, or null
if there is no such keyReturns the least key strictly greater than the given key, or null
if there is no such key.
key | the key |
---|
key
, or null
if there is no such keyReturns whether this map is empty.
This implementation compares size()
to 0.
true
if this map has no elements, false
otherwise.Returns a set of the keys contained in this Map
. The Set
is backed by this Map
so changes to one are reflected by the other. The Set
does not support adding.
This implementation returns a view that calls through this to map. Its iterator transforms this map's entry set iterator to return keys.
Returns a key-value mapping associated with the greatest key in this map, or null
if the map is empty.
null
if this map is empty Returns the last key in this sorted map.
Returns a key-value mapping associated with the greatest key strictly less than the given key, or null
if there is no such key.
key | the key |
---|
key
, or null
if there is no such keyReturns the greatest key strictly less than the given key, or null
if there is no such key.
key | the key |
---|
key
, or null
if there is no such keyReturns a 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.
Removes and returns a key-value mapping associated with the least key in this map, or null
if the map is empty.
null
if this map is empty Removes and returns a key-value mapping associated with the greatest key in this map, or null
if the map is empty.
null
if this map is empty Maps the specified key to the specified value.
This base implementation throws UnsupportedOperationException
.
key | the key. |
---|---|
value | the value. |
null
if there was no mapping.Removes a mapping with the specified key from this Map
.
This implementation iterates its entry set, removing the entry with a key that key
equals.
key | the key of the mapping to remove. |
---|
null
if no mapping for the specified key was found.Returns the number of mappings in this Map
.
This implementation returns its entry set's size.
Map
. Returns a sorted map over a range of this sorted map with all keys greater than or equal to the specified startKey
and less than the specified endKey
. Changes to the returned sorted map are reflected in this sorted map and vice versa.
Note: The returned map will not allow an insertion of a key outside the specified range.
Equivalent to subMap(fromKey, true, toKey, false)
.
fromInclusive | the low boundary of the range (inclusive). |
---|---|
toExclusive | the high boundary of the range (exclusive), |
Returns a view of the portion of this map whose keys range from fromKey
to toKey
. If fromKey
and toKey
are equal, the returned map is empty unless fromExclusive
and toExclusive
are both true. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.
The returned map will throw an IllegalArgumentException
on an attempt to insert a key outside of its range, or to construct a submap either of whose endpoints lie outside its range.
from | low endpoint of the keys in the returned map |
---|---|
fromInclusive | true if the low endpoint is to be included in the returned view |
to | high endpoint of the keys in the returned map |
toInclusive | true if the high endpoint is to be included in the returned view |
fromKey
to toKey
Returns a view of the portion of this map whose keys are greater than (or equal to, if inclusive
is true) fromKey
. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.
The returned map will throw an IllegalArgumentException
on an attempt to insert a key outside its range.
from | low endpoint of the keys in the returned map |
---|---|
inclusive | true if the low endpoint is to be included in the returned view |
inclusive
is true) fromKey
Returns a sorted map over a range of this sorted map with all keys that are greater than or equal to the specified startKey
. Changes to the returned sorted map are reflected in this sorted map and vice versa.
Note: The returned map will not allow an insertion of a key outside the specified range.
Equivalent to tailMap(fromKey, true)
.
fromInclusive | the low boundary of the range specified. |
---|
startKey
.