public class TreeMap<K,V> extends AbstractMap<K,V> implements SortedMap<K,V>, NavigableMap<K,V>, Cloneable, Serializable
put(K, V) and remove(java.lang.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<String, String> map = new TreeMap<String, String>(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"));
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>| Constructor and Description |
|---|
TreeMap()
Create a natural order, empty tree map whose keys must be mutually
comparable and non-null.
|
TreeMap(Comparator<? super K> comparator)
Create a tree map ordered by
comparator. |
TreeMap(Map<? extends K,? extends V> copyFrom)
Create a natural order tree map populated with the key/value pairs of
copyFrom. |
TreeMap(SortedMap<K,? extends V> copyFrom)
Create a tree map with the ordering and key/value pairs of
copyFrom. |
| Modifier and Type | Method and Description |
|---|---|
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. |
void |
clear()
Removes all of the mappings from this map (optional operation).
|
Object |
clone()
Creates and returns a copy of this
Object. |
Comparator<? super K> |
comparator()
Returns the comparator used to order the keys in this map, or
null if this map uses the natural ordering of its keys. |
boolean |
containsKey(Object key)
Returns
true if this map contains a mapping for the specified
key. |
NavigableSet<K> |
descendingKeySet()
Returns a reverse order
NavigableSet view of the keys contained in this map. |
NavigableMap<K,V> |
descendingMap()
Returns a reverse order view of the mappings contained in this map.
|
Set<Map.Entry<K,V>> |
entrySet()
Returns a
Set view of the mappings contained in this map. |
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. |
V |
get(Object key)
Returns the value to which the specified key is mapped,
or
null if this map contains no mapping for the key. |
SortedMap<K,V> |
headMap(K toExclusive)
Returns a view of the portion of this map whose keys are
strictly less than
toKey. |
NavigableMap<K,V> |
headMap(K to,
boolean inclusive)
Returns a view of the portion of this map whose keys are less than (or
equal to, if
inclusive is true) 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. |
boolean |
isEmpty()
Returns
true if this map contains no key-value mappings. |
Set<K> |
keySet()
Returns a
Set view of the keys contained 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. |
NavigableSet<K> |
navigableKeySet()
Returns a
NavigableSet view of the keys contained in this map. |
Map.Entry<K,V> |
pollFirstEntry()
Removes and returns a key-value mapping associated with
the least key in this map, or
null if the map is empty. |
Map.Entry<K,V> |
pollLastEntry()
Removes and returns a key-value mapping associated with
the greatest key in this map, or
null if the map is empty. |
V |
put(K key,
V value)
Associates the specified value with the specified key in this map
(optional operation).
|
V |
remove(Object key)
Removes the mapping for a key from this map if it is present
(optional operation).
|
int |
size()
Returns the number of key-value mappings in this map.
|
NavigableMap<K,V> |
subMap(K from,
boolean fromInclusive,
K to,
boolean toInclusive)
Returns a view of the portion of this map whose keys range from
fromKey to toKey. |
SortedMap<K,V> |
subMap(K fromInclusive,
K toExclusive)
Returns a view of the portion of this map whose keys range from
fromKey, inclusive, to toKey, exclusive. |
SortedMap<K,V> |
tailMap(K fromInclusive)
Returns a view of the portion of this map whose keys are
greater than or equal to
fromKey. |
NavigableMap<K,V> |
tailMap(K from,
boolean inclusive)
Returns a view of the portion of this map whose keys are greater than (or
equal to, if
inclusive is true) fromKey. |
containsValue, equals, hashCode, putAll, toString, valuesfinalize, getClass, notify, notifyAll, wait, wait, waitcompute, computeIfAbsent, computeIfPresent, containsValue, copyOf, entry, equals, forEach, getOrDefault, hashCode, merge, of, of, of, of, of, of, of, of, of, of, of, ofEntries, putAll, putIfAbsent, remove, replace, replace, replaceAllpublic TreeMap()
public TreeMap(Map<? extends K,? extends V> copyFrom)
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<String, Integer> customOrderedMap
= new TreeMap<String, Integer>(copyFrom.comparator());
customOrderedMap.putAll(copyFrom);
public TreeMap(Comparator<? super K> comparator)
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.public TreeMap(SortedMap<K,? extends V> copyFrom)
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<String, Integer> customOrderedMap
= new TreeMap<String, Integer>(copyFrom.comparator());
customOrderedMap.putAll(copyFrom);
public Object clone()
ObjectObject. 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.clone in class AbstractMap<K,V>public int size()
AbstractMapInteger.MAX_VALUE elements, returns
Integer.MAX_VALUE.
This implementation returns its entry set's size.
public boolean isEmpty()
AbstractMaptrue if this map contains no key-value mappings.
This implementation compares size() to 0.
public V get(Object key)
AbstractMapnull if this map contains no mapping for the key.
More formally, if this map contains a mapping from a key
k to a value v such that
Objects.equals(key, k),
then this method returns v; otherwise
it returns null. (There can be at most one such mapping.)
If this map permits null values, then a return value of
null does not necessarily indicate that the map
contains no mapping for the key; it's also possible that the map
explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.
This implementation iterates its entry set, looking for an entry with
a key that key equals.
public boolean containsKey(Object key)
AbstractMaptrue if this map contains a mapping for the specified
key. More formally, returns true if and only if
this map contains a mapping for a key k such that
Objects.equals(key, k). (There can be
at most one such mapping.)
This implementation iterates its key set, looking for a key that
key equals.
containsKey in interface Map<K,V>containsKey in class AbstractMap<K,V>key - key whose presence in this map is to be testedtrue if this map contains a mapping for the specified
keypublic V put(K key, V value)
AbstractMapm is said to contain a mapping for a key k if and only
if m.containsKey(k) would return
true.)
This base implementation throws UnsupportedOperationException.
put in interface Map<K,V>put in class AbstractMap<K,V>key - key with which the specified value is to be associatedvalue - value to be associated with the specified keykey, or
null if there was no mapping for key.
(A null return can also indicate that the map
previously associated null with key,
if the implementation supports null values.)public void clear()
AbstractMapThis implementation calls entrySet().clear().
public V remove(Object key)
AbstractMapk to value v such that
Objects.equals(key, k), that mapping
is removed. (The map can contain at most one such mapping.)
Returns the value to which this map previously associated the key,
or null if the map contained no mapping for the key.
If this map permits null values, then a return value of
null does not necessarily indicate that the map
contained no mapping for the key; it's also possible that the map
explicitly mapped the key to null.
The map will not contain a mapping for the specified key once the call returns.
This implementation iterates its entry set, removing the entry with
a key that key equals.
public Map.Entry<K,V> firstEntry()
NavigableMapnull if the map is empty.firstEntry in interface NavigableMap<K,V>null if this map is emptypublic Map.Entry<K,V> pollFirstEntry()
NavigableMapnull if the map is empty.pollFirstEntry in interface NavigableMap<K,V>null if this map is emptypublic K firstKey()
SortedMappublic Map.Entry<K,V> lastEntry()
NavigableMapnull if the map is empty.lastEntry in interface NavigableMap<K,V>null if this map is emptypublic Map.Entry<K,V> pollLastEntry()
NavigableMapnull if the map is empty.pollLastEntry in interface NavigableMap<K,V>null if this map is emptypublic K lastKey()
SortedMappublic Map.Entry<K,V> lowerEntry(K key)
NavigableMapnull 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)
NavigableMapnull 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)
NavigableMapnull 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)
NavigableMapnull 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)
NavigableMapnull 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)
NavigableMapnull 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)
NavigableMapnull 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)
NavigableMapnull if there is no such key.higherKey in interface NavigableMap<K,V>key - the keykey,
or null if there is no such keypublic Comparator<? super K> comparator()
SortedMapnull if this map uses the natural ordering of its keys.comparator in interface SortedMap<K,V>null if this map uses the natural ordering
of its keyspublic Set<Map.Entry<K,V>> entrySet()
MapSet view of the mappings contained in this map.
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, or through the
setValue operation on a map entry returned by the
iterator) 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.public Set<K> keySet()
AbstractMapSet view of the keys contained in this map.
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.
This implementation returns a view that calls through this to map. Its iterator transforms this map's entry set iterator to return keys.
public NavigableSet<K> navigableKeySet()
NavigableMapNavigableSet 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 NavigableMap<K,V> subMap(K from, boolean fromInclusive, K to, boolean toInclusive)
NavigableMapfromKey to toKey. If fromKey and
toKey are equal, the returned map is empty unless
fromInclusive and toInclusive 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.
subMap in interface NavigableMap<K,V>from - low endpoint of the keys in the returned mapfromInclusive - true if the low endpoint
is to be included in the returned viewto - high endpoint of the keys in the returned maptoInclusive - true if the high endpoint
is to be included in the returned viewfromKey to toKeypublic SortedMap<K,V> subMap(K fromInclusive, K toExclusive)
SortedMapfromKey, inclusive, to toKey, exclusive. (If
fromKey and toKey are equal, the returned map
is empty.) 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.
subMap in interface NavigableMap<K,V>subMap in interface SortedMap<K,V>fromInclusive - low endpoint (inclusive) of the keys in the returned maptoExclusive - high endpoint (exclusive) of the keys in the returned mapfromKey, inclusive, to toKey, exclusivepublic NavigableMap<K,V> headMap(K to, boolean inclusive)
NavigableMapinclusive 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.
headMap in interface NavigableMap<K,V>to - high endpoint of the keys in the returned mapinclusive - true if the high endpoint
is to be included in the returned viewinclusive is true) toKeypublic SortedMap<K,V> headMap(K toExclusive)
SortedMaptoKey. 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.
public NavigableMap<K,V> tailMap(K from, boolean inclusive)
NavigableMapinclusive 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.
tailMap in interface NavigableMap<K,V>from - low endpoint of the keys in the returned mapinclusive - true if the low endpoint
is to be included in the returned viewinclusive is true) fromKeypublic SortedMap<K,V> tailMap(K fromInclusive)
SortedMapfromKey. 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.
public NavigableMap<K,V> descendingMap()
NavigableMapremove
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 NavigableSet<K> descendingKeySet()
NavigableMapNavigableSet 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>