TreeMap

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

class TreeMap : AbstractMap!(K, V), NavigableMap!(K, V)(
K
V
) if (
isOrderingComparable!K
) {}

Constructors

this
this()

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}.

this
this(Comparator!K comparator)

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}.

this
this(Map!(K, V) m)

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.

Members

Classes

KeyInputRange
class KeyInputRange
Undocumented in source.
ValueInputRange
class ValueInputRange
Undocumented in source.

Functions

byKey
InputRange!K byKey()
Undocumented in source. Be warned that the author may not have intended to support it.
byValue
InputRange!V byValue()
Undocumented in source. Be warned that the author may not have intended to support it.
ceilingEntry
MapEntry!(K, V) ceilingEntry(K key)

@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

ceilingKey
K ceilingKey(K key)

@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

clear
void clear()

Removes all of the mappings from this map. The map will be empty after this call returns.

clone
Object clone()
Undocumented in source. Be warned that the author may not have intended to support it.
comparator
Comparator!K comparator()
Undocumented in source. Be warned that the author may not have intended to support it.
containsKey
bool containsKey(K key)

Returns {@code true} if this map contains a mapping for the specified key.

containsValue
bool containsValue(V value)

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.

firstEntry
MapEntry!(K, V) firstEntry()
firstKey
K firstKey()

@throws NoSuchElementException {@inheritDoc}

floorEntry
MapEntry!(K, V) floorEntry(K key)

@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

floorKey
K floorKey(K key)

@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

get
V get(K key)

Returns the value to which the specified key is mapped, or {@code null} if this map contains no mapping for the key.

getCeilingEntry
TreeMapEntry!(K, V) getCeilingEntry(K 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}.

getEntry
TreeMapEntry!(K, V) getEntry(K key)

Returns this map's entry for the given key, or {@code null} if the map does not contain an entry for the key.

getEntryUsingComparator
TreeMapEntry!(K, V) getEntryUsingComparator(K 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.)

getFirstEntry
TreeMapEntry!(K, V) getFirstEntry()

Returns the first Entry in the TreeMap (according to the TreeMap's key-sort function). Returns null if the TreeMap is empty.

getFloorEntry
TreeMapEntry!(K, V) getFloorEntry(K key)

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}.

getHigherEntry
TreeMapEntry!(K, V) getHigherEntry(K key)

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}.

getLastEntry
TreeMapEntry!(K, V) getLastEntry()

Returns the last Entry in the TreeMap (according to the TreeMap's key-sort function). Returns null if the TreeMap is empty.

getLowerEntry
TreeMapEntry!(K, V) 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 the specified key), returns {@code null}.

headMap
NavigableMap!(K, V) headMap(K toKey, bool inclusive)

@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}

headMap
SortedMap!(K, V) headMap(K toKey)

@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}

higherEntry
MapEntry!(K, V) higherEntry(K key)

@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

higherKey
K higherKey(K key)

@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

lastEntry
MapEntry!(K, V) lastEntry()
lastKey
K lastKey()

@throws NoSuchElementException {@inheritDoc}

lowerEntry
MapEntry!(K, V) lowerEntry(K key)

@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

lowerKey
K lowerKey(K key)

@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

opApply
int opApply(int delegate(ref K, ref V) dg)
Undocumented in source. Be warned that the author may not have intended to support it.
opApply
int opApply(int delegate(MapEntry!(K, V) entry) dg)
Undocumented in source. Be warned that the author may not have intended to support it.
opEquals
bool opEquals(IObject o)

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.

opEquals
bool opEquals(Object o)
Undocumented in source. Be warned that the author may not have intended to support it.
pollFirstEntry
MapEntry!(K, V) pollFirstEntry()
pollLastEntry
MapEntry!(K, V) pollLastEntry()
put
V put(K key, V value)

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.

putAll
void putAll(Map!(K, V) map)

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.

remove
V remove(K key)

Removes the mapping for this key from this TreeMap if present.

replace
bool replace(K key, V oldValue, V newValue)
Undocumented in source. Be warned that the author may not have intended to support it.
replace
V replace(K key, V value)
Undocumented in source. Be warned that the author may not have intended to support it.
subMap
NavigableMap!(K, V) subMap(K fromKey, bool fromInclusive, K toKey, bool toInclusive)

@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}

subMap
SortedMap!(K, V) subMap(K fromKey, K toKey)

@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}

tailMap
NavigableMap!(K, V) tailMap(K fromKey, bool inclusive)

@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}

tailMap
SortedMap!(K, V) tailMap(K fromKey)

@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}

toHash
size_t toHash()
Undocumented in source. Be warned that the author may not have intended to support it.
toString
string toString()
Undocumented in source. Be warned that the author may not have intended to support it.

Mixin templates

TreeMapIterator
mixintemplate TreeMapIterator()

Base class for TreeMap Iterators

Meta