Constructs a new, empty tree map, using the natural ordering of its keys. All keys inserted into the map must implement the {@link Comparable} interface. Furthermore, all such keys must be <em>mutually comparable</em>: {@code k1.compareTo(k2)} must not throw a {@code ClassCastException} for any keys {@code k1} and {@code k2} in the map. If the user attempts to put a key into the map that violates this constraint (for example, the user attempts to put a string key into a map whose keys are integers), the {@code put(Object key, Object value)} call will throw a {@code ClassCastException}.
Constructs a new, empty tree map, ordered according to the given comparator. All keys inserted into the map must be <em>mutually comparable</em> by the given comparator: {@code comparator.compare(k1, k2)} must not throw a {@code ClassCastException} for any keys {@code k1} and {@code k2} in the map. If the user attempts to put a key into the map that violates this constraint, the {@code put(Object key, Object value)} call will throw a {@code ClassCastException}.
Constructs a new tree map containing the same mappings as the given map, ordered according to the <em>natural ordering</em> of its keys. All keys inserted into the new map must implement the {@link Comparable} interface. Furthermore, all such keys must be <em>mutually comparable</em>: {@code k1.compareTo(k2)} must not throw a {@code ClassCastException} for any keys {@code k1} and {@code k2} in the map. This method runs in n*log(n) time.
@throws ClassCastException {@inheritDoc} @throws NullPointerException if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys
@throws ClassCastException {@inheritDoc} @throws NullPointerException if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys
Removes all of the mappings from this map. The map will be empty after this call returns.
Returns {@code true} if this map contains a mapping for the specified key.
Returns {@code true} if this map maps one or more keys to the specified value. More formally, returns {@code true} if and only if this map contains at least one mapping to a value {@code v} such that {@code (value is null ? v is null : value.equals(v))}. This operation will probably require time linear in the map size for most implementations.
@throws NoSuchElementException {@inheritDoc}
@throws ClassCastException {@inheritDoc} @throws NullPointerException if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys
@throws ClassCastException {@inheritDoc} @throws NullPointerException if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys
Returns the value to which the specified key is mapped, or {@code null} if this map contains no mapping for the key.
Gets the entry corresponding to the specified key; if no such entry exists, 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 the specified key), returns {@code null}.
Returns this map's entry for the given key, or {@code null} if the map does not contain an entry for the key.
Version of getEntry using comparator. Split off from getEntry for performance. (This is not worth doing for most methods, that are less dependent on comparator performance, but is worthwhile here.)
Returns the first Entry in the TreeMap (according to the TreeMap's key-sort function). Returns null if the TreeMap is empty.
Gets the entry corresponding to the specified key; if no such entry exists, returns the entry for the greatest key less than the specified key; if no such entry exists, returns {@code null}.
Gets the entry for the least key greater than the specified key; if no such entry exists, returns the entry for the least key greater than the specified key; if no such entry exists returns {@code null}.
Returns the last Entry in the TreeMap (according to the TreeMap's key-sort function). Returns null if the TreeMap is empty.
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 the specified key), returns {@code null}.
@throws ClassCastException {@inheritDoc} @throws NullPointerException if {@code toKey} is null and this map uses natural ordering, or its comparator does not permit null keys @throws IllegalArgumentException {@inheritDoc}
@throws ClassCastException {@inheritDoc} @throws NullPointerException if {@code toKey} is null and this map uses natural ordering, or its comparator does not permit null keys @throws IllegalArgumentException {@inheritDoc}
@throws ClassCastException {@inheritDoc} @throws NullPointerException if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys
@throws ClassCastException {@inheritDoc} @throws NullPointerException if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys
@throws NoSuchElementException {@inheritDoc}
@throws ClassCastException {@inheritDoc} @throws NullPointerException if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys
@throws ClassCastException {@inheritDoc} @throws NullPointerException if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys
Base class for spliterators. Iteration starts at a given origin and continues up to but not including a given fence (or null for end). At top-level, for ascending cases, the first split uses the root as left-fence/right-origin. From there, right-hand splits replace the current fence with its left child, also serving as origin for the split-off spliterator. Left-hands are symmetric. Descending versions place the origin at the end and invert ascending split rules. This base class is non-commital about directionality, or whether the top-level spliterator covers the whole tree. This means that the actual split mechanics are located in subclasses. Some of the subclass trySplit methods are identical (except for return types), but not nicely factorable.
Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.
Copies all of the mappings from the specified map to this map. These mappings replace any mappings that this map had for any of the keys currently in the specified map.
Removes the mapping for this key from this TreeMap if present.
@throws ClassCastException {@inheritDoc} @throws NullPointerException if {@code fromKey} or {@code toKey} is null and this map uses natural ordering, or its comparator does not permit null keys @throws IllegalArgumentException {@inheritDoc}
@throws ClassCastException {@inheritDoc} @throws NullPointerException if {@code fromKey} or {@code toKey} is null and this map uses natural ordering, or its comparator does not permit null keys @throws IllegalArgumentException {@inheritDoc}
@throws ClassCastException {@inheritDoc} @throws NullPointerException if {@code fromKey} is null and this map uses natural ordering, or its comparator does not permit null keys @throws IllegalArgumentException {@inheritDoc}
@throws ClassCastException {@inheritDoc} @throws NullPointerException if {@code fromKey} is null and this map uses natural ordering, or its comparator does not permit null keys @throws IllegalArgumentException {@inheritDoc}
Base class for TreeMap Iterators
A Red-Black tree based {@link NavigableMap} implementation. The map is sorted according to the {@linkplain Comparable natural ordering} of its keys, or by a {@link Comparator} provided at map creation time, depending on which constructor is used.
<p>This implementation provides guaranteed log(n) time cost for the {@code containsKey}, {@code get}, {@code put} and {@code remove} operations. Algorithms are adaptations of those in Cormen, Leiserson, and Rivest's <em>Introduction to Algorithms</em>.
<p>Note that the ordering maintained by a tree map, like any sorted map, and whether or not an explicit comparator is provided, must be <em>consistent with {@code equals}</em> if this sorted map is to correctly implement the {@code Map} interface. (See {@code Comparable} or {@code Comparator} for a precise definition of <em>consistent with equals</em>.) This is so because the {@code Map} interface is defined in terms of the {@code equals} operation, but a sorted map performs all key comparisons using its {@code compareTo} (or {@code 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 <em>is</em> well-defined even if its ordering is inconsistent with {@code equals}; it just fails to obey the general contract of the {@code Map} interface.
<p><strong>Note that this implementation is not synchronized.</strong> If multiple threads access a map concurrently, and at least one of the threads modifies the map structurally, it <em>must</em> 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 {@link Collections#synchronizedSortedMap Collections.synchronizedSortedMap} method. This is best done at creation time, to prevent accidental unsynchronized access to the map: <pre> SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));</pre>
<p>The iterators returned by the {@code iterator} method of the collections returned by all of this class's "collection view methods" are <em>fail-fast</em>: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own {@code remove} method, the iterator will throw a {@link 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.
<p>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 {@code ConcurrentModificationException} on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: <em>the fail-fast behavior of iterators should be used only to detect bugs.</em>
<p>All {@code MapEntry} pairs returned by methods in this class and its views represent snapshots of mappings at the time they were produced. They do <strong>not</strong> support the {@code Entry.setValue} method. (Note however that it is possible to change mappings in the associated map using {@code put}.)
<p>This class is a member of the <a href="{@docRoot}/../technotes/guides/collections/index.html"> Java Collections Framework</a>.
@param !K the type of keys maintained by this map @param !V the type of mapped values
@author Josh Bloch and Doug Lea @see Map @see HashMap @see Hashtable @see Comparable @see Comparator @see Collection