TreeMap

A Red-Black tree based implementation of the MutableMap interface, striving for compatibility with the subset of TreeMap functionality potentially used by Lucene.

The map is sorted according to the natural ordering of its keys (if they implement Comparable), or by a Comparator provided at map creation time.

This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations.

Note that this implementation is not synchronized. External synchronization is required if multiple threads access a map concurrently and at least one modifies it structurally.

Iterators are fail-fast: they throw ConcurrentModificationException if the map is structurally modified after iterator creation, except through the iterator's own remove method.

Type Parameters

K

the type of keys maintained by this map

V

the type of mapped values

Constructors

Link copied to clipboard
constructor()

Constructs a new, empty tree map, using the natural ordering of its keys. All keys inserted into the map must implement the Comparable interface and be mutually comparable.

constructor(comparator: Comparator<K>?)

Constructs a new, empty tree map, ordered according to the given comparator. All keys inserted into the map must be mutually comparable by the given comparator.

constructor(m: Map<out K, V>)

Constructs a new tree map containing the same mappings as the given map, ordered according to the natural ordering of its keys. Requires keys to be Comparable. Runs in n*log(n) time.

constructor(m: TreeMap<K, out V>)

Constructs a new tree map containing the same mappings and using the same ordering as the specified sorted map (approximated by checking if the input map is also a TreeMap with the same comparator). If compatible, this method runs in linear time (O(n)). Otherwise, it falls back to O(n log n).

Types

Link copied to clipboard
object Companion
Link copied to clipboard
class Entry<K, V>(var key: K, var value: V, var parent: TreeMap.Entry<K, V>?) : MutableMap.MutableEntry<K, V>
Link copied to clipboard

Properties

Link copied to clipboard

Returns a MutableSet view of the mappings contained in this map. The set's iterator returns the entries in ascending key 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, 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.

Link copied to clipboard

Returns a Set view of the mappings contained in this map. The set's iterator returns the entries in ascending key 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, 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.

Link copied to clipboard
open override val keys: MutableSet<K>

Returns a MutableSet 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.

Link copied to clipboard
open override val keySet: MutableSet<K>

Returns a Set 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.

Link copied to clipboard
open override var size: Int

The number of entries in the tree.

Link copied to clipboard
open override val values: MutableCollection<V>

Returns a {@link Collection} view of the values contained in this map.

Functions

Link copied to clipboard
fun addAllForTreeSet(set: SortedSet<K>, defaultVal: V)
Link copied to clipboard
open override fun ceilingEntry(key: K): Map.Entry<K, V>?

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.

Link copied to clipboard
open override fun ceilingKey(key: K): K?

Returns the least key greater than or equal to the given key, or null if there is no such key.

Link copied to clipboard
open override fun clear()

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

Link copied to clipboard
open override fun comparator(): Comparator<K>?

Returns the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.

Link copied to clipboard
fun compute(key: K, remappingFunction: (K, V?) -> V?): V?

Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping). If the function returns null, the mapping is removed (or remains absent).

Link copied to clipboard
fun computeIfAbsent(key: K, mappingFunction: (K) -> V?): V?

If the specified key is not already associated with a value (or is mapped to null), computes its value using the given mapping function and enters it into this map unless null.

Link copied to clipboard
fun <K, V> MutableMap<K, V>.computeIfAbsent(key: K, mappingFunction: (K) -> V): V?

ported from java.util.Map.computeIfAbsent()

Link copied to clipboard
fun computeIfPresent(key: K, remappingFunction: (K, V) -> V?): V?

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. If the function returns null, the mapping is removed.

Link copied to clipboard
open override fun containsKey(key: K): Boolean

Returns true if this map contains a mapping for the specified key.

Link copied to clipboard
open override fun containsValue(value: V): Boolean

Returns true if this map maps one or more keys to the specified value. This operation requires time linear in the map size.

Link copied to clipboard
open override fun descendingKeySet(): NavigableSet<K>

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.

Link copied to clipboard
open override fun descendingMap(): NavigableMap<K, V>

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.

Link copied to clipboard
open override fun firstEntry(): Map.Entry<K, V>?

Returns the first (lowest) key-value mapping currently in this map.

Link copied to clipboard
open override fun firstKey(): K

Returns the first (lowest) key currently in this map.

Link copied to clipboard
open override fun floorEntry(key: K): Map.Entry<K, V>?

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.

Link copied to clipboard
open override fun floorKey(key: K): K?

Returns the greatest key less than or equal to the given key, or null if there is no such key.

Link copied to clipboard
open operator override fun get(key: K): V?

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

Link copied to clipboard
open override fun headMap(toKey: K): SortedMap<K, V>

{@inheritDoc}

open override fun headMap(toKey: K, inclusive: Boolean): NavigableMap<K, V>
Link copied to clipboard
open override fun higherEntry(key: K): Map.Entry<K, V>?

Returns a key-value mapping associated with the least key strictly greater than the given key, or null if there is no such key.

Link copied to clipboard
open override fun higherKey(key: K): K?

Returns the least key strictly greater than the given key, or null if there is no such key.

Link copied to clipboard
open fun isEmpty(): Boolean
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open override fun lastEntry(): Map.Entry<K, V>?

Returns the last (highest) key-value mapping currently in this map.

Link copied to clipboard
open override fun lastKey(): K

Returns the last (highest) key currently in this map.

Link copied to clipboard
open override fun lowerEntry(key: K): Map.Entry<K, V>?

Returns a key-value mapping associated with the greatest key strictly less than the given key, or null if there is no such key.

Link copied to clipboard
open override fun lowerKey(key: K): K?

Returns the greatest key strictly less than the given key, or null if there is no such key.

Link copied to clipboard
fun merge(key: K, value: V, remappingFunction: (V, V) -> V?): V?

If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value. Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result is null.

Link copied to clipboard
open override fun navigableKeySet(): NavigableSet<K>

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

Link copied to clipboard
open override fun pollFirstEntry(): Map.Entry<K, V>?

Removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.

Link copied to clipboard
open override fun pollLastEntry(): Map.Entry<K, V>?

Removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.

Link copied to clipboard
open override fun put(key: K, value: V): V?

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.

Link copied to clipboard
open override fun putAll(from: Map<out K, V>)

Copies all of the mappings from the specified map to this map.

Link copied to clipboard
open override fun putFirst(k: K, v: V): V

Throws UnsupportedOperationException. The encounter order induced by this map's comparison method determines the position of mappings, so explicit positioning is not supported.

Link copied to clipboard
fun putIfAbsent(key: K, value: V): V?

If the specified key is not already associated with a value (or is mapped to null), associates it with the given value and returns null, else returns the current value.

Link copied to clipboard
fun <K, V> MutableMap<K, V>.putIfAbsent(key: K, value: V): V?

ported from java.util.Map.putIfAbsent()

Link copied to clipboard
open override fun putLast(k: K, v: V): V

Throws UnsupportedOperationException. The encounter order induced by this map's comparison method determines the position of mappings, so explicit positioning is not supported.

Link copied to clipboard
open override fun remove(key: K): V?

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

fun remove(key: K, value: V): Boolean

Removes the entry for the specified key only if it is currently mapped to the specified value.

Link copied to clipboard
fun <K, V> MutableMap<K, V>.remove(key: K, value: V): Boolean

ported from java.util.Map.remove()

Link copied to clipboard
fun replace(key: K, value: V): V?

Replaces the entry for the specified key only if it is currently mapped to some value.

fun replace(key: K, oldValue: V, newValue: V): Boolean

Replaces the entry for the specified key only if currently mapped to the specified value.

Link copied to clipboard
fun <K, V> MutableMap<K, V>.replace(key: K, value: V): V?

ported from java.util.Map.replace()

Link copied to clipboard
open override fun reversed(): NavigableMap<K, V>

{@inheritDoc}

Link copied to clipboard
open override fun subMap(fromKey: K, toKey: K): SortedMap<K, V>

{@inheritDoc}

open override fun subMap(fromKey: K, fromInclusive: Boolean, toKey: K, toInclusive: Boolean): SortedMap<K, V>

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

Link copied to clipboard
open override fun tailMap(fromKey: K): SortedMap<K, V>

{@inheritDoc}

open override fun tailMap(fromKey: K, inclusive: Boolean): SortedMap<K, V>

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.