NavigableMap implementation.
The map is sorted according to the natural
ordering of its keys, or by a Comparator provided at map
creation time, depending on which constructor is used.
This implementation provides guaranteed log(n) time cost for the
containsKey, get, put and remove
operations. Algorithms are adaptations of those in Cormen, Leiserson, and
Rivest's Introduction to Algorithms.
Note that the ordering maintained by a tree map, like any sorted map, and
whether or not an explicit comparator is provided, must be consistent
with equals if this sorted map is to correctly implement the
Map interface. (See Comparable or Comparator for a
precise definition of consistent with equals.) This is so because
the Map interface is defined in terms of the equals
operation, but a sorted map performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by
this method are, from the standpoint of the sorted map, equal. The behavior
of a sorted map is well-defined even if its ordering is
inconsistent with equals; it just fails to obey the general contract
of the Map interface.
Note that this implementation is not synchronized.
If multiple threads access a map concurrently, and at least one of the
threads modifies the map structurally, it must be synchronized
externally. (A structural modification is any operation that adds or
deletes one or more mappings; merely changing the value associated
with an existing key is not a structural modification.) This is
typically accomplished by synchronizing on some object that naturally
encapsulates the map.
If no such object exists, the map should be "wrapped" using the
Collections.
method. This is best done at creation time, to prevent accidental
unsynchronized access to the map:
SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));
The iterators returned by the iterator method of the collections
returned by all of this class's "collection view methods" are
fail-fast: if the map is structurally modified at any time after
the iterator is created, in any way except through the iterator's own
remove method, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent
modification, the iterator fails quickly and cleanly, rather than risking
arbitrary, non-deterministic behavior at an undetermined time in the future.
Note that the fail-fast behavior of an iterator cannot be guaranteed
as it is, generally speaking, impossible to make any hard guarantees in the
presence of unsynchronized concurrent modification. Fail-fast iterators
throw ConcurrentModificationException on a best-effort basis.
Therefore, it would be wrong to write a program that depended on this
exception for its correctness: the fail-fast behavior of iterators
should be used only to detect bugs.
The methods
ceilingEntry,
firstEntry,
floorEntry,
higherEntry,
lastEntry,
lowerEntry,
pollFirstEntry, and
pollLastEntry
return Map. instances that represent snapshots of mappings as
of the time of the call. They do not support mutation of the
underlying map via the optional setValue method.
The putFirst and putLast methods of this class
throw UnsupportedOperationException. The encounter order of mappings is determined
by the comparison method; therefore, explicit positioning is not supported.
This class is a member of the Java Collections Framework.
Map, HashMap, Hashtable, Comparable, Comparator, Collection
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class | |
| pack-priv class | |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv class | |
| pack-priv class | |
| pack-priv static class | |
| pack-priv class | |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv abstract static class | |
| pack-priv abstract class | |
| private class | TreeMap.
This class exists solely for the sake of serialization compatibility with previous releases of TreeMap that did not support NavigableMap. |
| pack-priv static class | |
| pack-priv class | |
| pack-priv class | |
| pack-priv static class |
| Modifier and Type | Field and Description |
|---|---|
| private static final boolean | |
| private final Comparator | comparator
The comparator used to maintain order in this tree map, or null if it uses the natural ordering of its keys. |
| private transient NavigableMap | |
| private transient TreeMap | entrySet
Fields initialized to contain an instance of the entry set view the first time this view is requested. |
| private transient int | modCount
The number of structural modifications to the tree. |
| private transient TreeMap. | |
| private static final boolean | |
| private transient TreeMap. | |
| private static final long | |
| private transient int | size
The number of entries in the tree |
| private static final Object | UNBOUNDED
Dummy value serving as unmatchable fence key for unbounded SubMapIterators |
| Access | Constructor and Description |
|---|---|
| public | |
| public | TreeMap(Comparator<? super K>
the comparator that will be used to order this map.
If comparator)null, the natural
ordering of the keys will be used.Constructs a new, empty tree map, ordered according to the given comparator. |
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv void | addAllForTreeSet(SortedSet<? extends K> set, V defaultVal)
Intended to be called only from TreeSet.addAll |
| private void | |
| private void | |
| private void | buildFromSorted(int
the number of keys (or key-value pairs) to be read from
the iterator or stream size, Iterator<?> If non-null, new entries are created from entries
or keys read from this iterator. it, ObjectInputStream If non-null, new entries are created from keys and
possibly values read from this stream in serialized form.
Exactly one of it and str should be non-null. str, V if non-null, this default value is used for
each value in the map. If null, each value is read from
iterator or stream, as described above. defaultVal)Linear time tree building algorithm from sorted data. |
| private final TreeMap. | buildFromSorted(int
the current level of tree. Initial call should be 0. level, int the first element index of this subtree. Initial should be 0. lo, int the last element index of this subtree. Initial should be
size-1. hi, int the level at which nodes should be red.
Must be equal to computeRedLevel for tree of this size. redLevel, Iterator<?> it, ObjectInputStream str, V defaultVal)Recursive "helper method" that does the real work of the previous method. |
| private V | |
| private V | callRemappingFunctionWithCheck(K key, V oldValue, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
|
| public Map. | ceilingEntry(K key)
Implements java. Returns a key-value mapping associated with the least key
greater than or equal to the given key, or |
| public K | ceilingKey(K key)
Implements java. Returns the least key greater than or equal to the given key,
or |
| public void | clear()
Overrides java. Implements java. Removes all of the mappings from this map. |
| public Object | Returns: a shallow copy of this mapOverrides java. Returns a shallow copy of this |
| private static <K, V> boolean | |
| public Comparator | comparator()
Implements java. Returns the comparator used to order the keys in this map, or
|
| pack-priv final int | |
| public V | compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
Overrides default java. Attempts to compute a mapping for the specified key and its current
mapped value, or |
| public V | computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)
Overrides default java. If the specified key is not already associated with a value (or is mapped
to |
| public V | computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
Overrides default java. If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value (optional operation). |
| private static int | computeRedLevel(int
the (non-negative) number of keys in the tree to be built size)Finds the level down to which to assign all nodes BLACK. |
| public boolean | Returns: true if this map contains a mapping for the
specified keykey whose presence in this map is to be tested key)Overrides java. Implements java. Returns |
| public boolean | Returns: true if a mapping to value exists;
false otherwisevalue whose presence in this map is to be tested value)Overrides java. Implements java. Returns |
| private void | |
| pack-priv Iterator | |
| public NavigableSet | descendingKeySet()
Implements java. Returns a reverse order |
| pack-priv final Spliterator | |
| public NavigableMap | descendingMap()
Implements java. Returns a reverse order view of the mappings contained in this map. |
| public Set | entrySet()
Implements abstract java. Implements java. Returns a |
| pack-priv static <K, V> Map. | |
| public Map. | firstEntry()
Implements java. Returns a key-value mapping associated with the least
key in this map, or |
| public K | firstKey()
Implements java. Returns the first (lowest) key currently in this map. |
| private void | |
| private void | |
| public Map. | floorEntry(K key)
Implements java. Returns a key-value mapping associated with the greatest key
less than or equal to the given key, or |
| public K | floorKey(K key)
Implements java. Returns the greatest key less than or equal to the given key,
or |
| public void | forEach(BiConsumer<? super K, ? super V> action)
Overrides default java. Performs the given action for each entry in this map until all entries have been processed or the action throws an exception. |
| public V | get(Object key)
Overrides java. Implements java. Returns the value to which the specified key is mapped,
or |
| pack-priv final TreeMap. | getCeilingEntry(K key)
Returns the entry for the least key greater than or equal to the specified key;
if no such entry exists (i.e. the specified key is greater than any key in the tree,
or the tree is empty), returns |
| pack-priv final TreeMap. | |
| pack-priv final TreeMap. | |
| pack-priv final TreeMap. | getFirstEntry()
Returns the first Entry in the TreeMap (according to the TreeMap's key-sort function). |
| pack-priv final TreeMap. | getFloorEntry(K key)
Returns the entry for the greatest key less than or equal to the specified key;
if no such entry exists (i.e. the specified key is less than any key in the tree,
or the tree is empty), returns |
| pack-priv final TreeMap. | getHigherEntry(K key)
Returns the entry for the least key greater than the specified key; if
no such entry exists (i.e., the greatest key in the Tree is less than
or equal to the specified key), returns |
| pack-priv final TreeMap. | |
| pack-priv final TreeMap. | getLowerEntry(K key)
Returns the entry for the greatest key less than the specified key; if
no such entry exists (i.e., the least key in the Tree is greater than
or equal to the specified key), returns |
| public NavigableMap | headMap(K toKey, boolean inclusive)
Implements java. Returns a view of the portion of this map whose keys are less than (or
equal to, if |
| public SortedMap | headMap(K toKey)
Implements java. Returns a view of the portion of this map whose keys are
strictly less than |
| public Map. | higherEntry(K key)
Implements java. Returns a key-value mapping associated with the least key
strictly greater than the given key, or |
| public K | higherKey(K key)
Implements java. Returns the least key strictly greater than the given key, or
|
| pack-priv static <K> K | |
| pack-priv Iterator | |
| pack-priv static <K, V> K | |
| public Set | keySet()
Overrides java. Implements java. Returns a |
| pack-priv final Spliterator | |
| pack-priv static <K> Spliterator | keySpliteratorFor(NavigableMap<K, ?> m)
Currently, we support Spliterator-based versions only for the full map, in either plain of descending form, otherwise relying on defaults because size estimation for submaps would dominate costs. |
| public Map. | lastEntry()
Implements java. Returns a key-value mapping associated with the greatest
key in this map, or |
| public K | lastKey()
Implements java. Returns the last (highest) key currently in this map. |
| private static <K, V> TreeMap. | |
| public Map. | lowerEntry(K key)
Implements java. Returns a key-value mapping associated with the greatest key
strictly less than the given key, or |
| public K | lowerKey(K key)
Implements java. Returns the greatest key strictly less than the given key, or
|
| public V | merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)
Overrides default java. If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value (optional operation). |
| private V | mergeValue(TreeMap.
|
| public NavigableSet | navigableKeySet()
Implements java. Returns a |
| private static <K, V> TreeMap. | |
| public Map. | pollFirstEntry()
Implements java. Removes and returns a key-value mapping associated with
the least key in this map, or |
| public Map. | pollLastEntry()
Implements java. Removes and returns a key-value mapping associated with
the greatest key in this map, or |
| pack-priv static <K, V> TreeMap. | predecessor(TreeMap.
Returns the predecessor of the specified Entry, or null if no such. |
| public V | Returns: the previous value associated withkey, or
null if there was no mapping for key.
(A null return can also indicate that the map
previously associated null with key.)key with which the specified value is to be associated key, V value to be associated with the specified key value)Overrides java. Implements java. Associates the specified value with the specified key in this map. |
| private V | |
| public void | putAll(Map<? extends K, ? extends V>
mappings to be stored in this map map)Overrides java. Implements java. Copies all of the mappings from the specified map to this map. |
| public V | putFirst(K k, V v)
Overrides default java. Throws |
| public V | putIfAbsent(K key, V value)
Overrides default java. If the specified key is not already associated with a value (or is mapped
to |
| public V | putLast(K k, V v)
Overrides default java. Throws |
| private void | readObject(final ObjectInputStream s)
Reconstitute the |
| pack-priv void | readTreeSet(int size, ObjectInputStream s, V defaultVal)
Intended to be called only from TreeSet.readObject |
| private V | remapValue(TreeMap.
|
| public V | Returns: the previous value associated withkey, or
null if there was no mapping for key.
(A null return can also indicate that the map
previously associated null with key.)key for which mapping should be removed key)Overrides java. Implements java. Removes the mapping for this key from this TreeMap if present. |
| public boolean | replace(K key, V oldValue, V newValue)
Overrides default java. Replaces the entry for the specified key only if currently mapped to the specified value (optional operation). |
| public V | replace(K key, V value)
Overrides default java. Replaces the entry for the specified key only if it is currently mapped to some value (optional operation). |
| public void | replaceAll(BiFunction<? super K, ? super V, ? extends V> function)
Overrides default java. Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception (optional operation). |
| private static <K, V> TreeMap. | |
| private void | |
| private void | |
| private static <K, V> void | |
| public int | Returns: the number of key-value mappings in this mapOverrides java. Implements java. Returns the number of key-value mappings in this map. |
| public NavigableMap | subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)
Implements java. Returns a view of the portion of this map whose keys range from
|
| public SortedMap | subMap(K fromKey, K toKey)
Implements java. Returns a view of the portion of this map whose keys range from
|
| pack-priv static <K, V> TreeMap. | |
| public NavigableMap | tailMap(K fromKey, boolean inclusive)
Implements java. Returns a view of the portion of this map whose keys are greater than (or
equal to, if |
| public SortedMap | tailMap(K fromKey)
Implements java. Returns a view of the portion of this map whose keys are
greater than or equal to |
| pack-priv static final boolean | |
| public Collection | values()
Overrides java. Implements java. Returns a |
| private void | writeObject(ObjectOutputStream s)
Save the state of the |