Skip to content
Buy me a coffee

TreeMap

Defined in: src/map/TreeMap.ts:231

A NavigableMap stored in a red-black tree, sorted by key. Keys are ordered by the comparator given to the constructor, or naturally (numbers, strings, bigints, booleans, Dates, objects with compareTo) when there is none; the ordering, not equals(), decides which keys are equal.

get, put, remove, containsKey and every navigation method run in O(log n); lastKey is O(1) (the maximum is cached) and putting a key above the maximum finds its slot in O(1). Iteration is in key order. Under natural ordering null/undefined keys throw NullPointerException and keys of mixed types throw ClassCastException; a comparator may accept them. Values may be null. Views and iterators are live and fail-fast.

K

V

new TreeMap<K, V>(source?): TreeMap<K, V>

Defined in: src/map/TreeMap.ts:252

Creates an empty map, an empty map ordered by a comparator, or a copy of a map. With no argument (or null) keys are ordered naturally. A SortedMap source (anything with a comparator() method) keeps its comparator and order; any other Map source is ordered naturally. One signature covers all forms.

Map<K, V> | Comparator<K> | null

a comparator, a map to copy, or nothing for natural ordering

TreeMap<K, V>

IllegalArgumentException if source is neither a comparator nor a map

NullPointerException / ClassCastException if a copied key cannot be compared

AbstractMap.constructor

[INSPECT](): string

Defined in: src/abstract/AbstractMap.ts:187

Node’s util.inspect hook: console.log prints the toString() form.

string

AbstractMap.[INSPECT]


[iterator](): IterableIterator<[K, V]>

Defined in: src/map/TreeMap.ts:918

Returns a fail-fast iterator over the mappings as [key, value] pairs, in ascending key order.

IterableIterator<[K, V]>

NavigableMap.[iterator]

AbstractMap.[iterator]


ceilingEntry(key): MapEntry<K, V> | null

Defined in: src/map/TreeMap.ts:1186

Returns the entry with the least key greater than or equal to key (an immutable snapshot), or null. O(log n).

K

MapEntry<K, V> | null

NullPointerException if key is null or undefined under natural ordering and this map is not empty

NavigableMap.ceilingEntry


ceilingKey(key): K | null

Defined in: src/map/TreeMap.ts:1194

Returns the least key greater than or equal to key, or null. O(log n).

K

K | null

NullPointerException if key is null or undefined under natural ordering and this map is not empty

NavigableMap.ceilingKey


clear(): void

Defined in: src/map/TreeMap.ts:798

Removes all mappings. O(1).

void

NavigableMap.clear

AbstractMap.clear


clone(): TreeMap<K, V>

Defined in: src/map/TreeMap.ts:266

Shallow copy of the same runtime class (subclass fields included) with the same comparator.

TreeMap<K, V>


comparator(): Comparator<K> | null

Defined in: src/map/TreeMap.ts:1079

Returns the comparator that orders the keys, or null when natural ordering is used.

Comparator<K> | null

NavigableMap.comparator


compute(key, fn): V | null

Defined in: src/map/TreeMap.ts:879

Stores fn(key, current), where current is the mapped value or null; a null/undefined result removes the mapping (or leaves the key absent). O(log n) plus the cost of fn.

K

(key, value) => V | null | undefined

V | null

the new value, or null if there is now no mapping

NullPointerException if fn is null, or key is null under natural ordering and must be compared or stored

ConcurrentModificationException if fn structurally modifies this map

NavigableMap.compute

AbstractMap.compute


computeIfAbsent(key, fn): V | null

Defined in: src/map/TreeMap.ts:840

If key is absent or mapped to null, stores fn(key) unless that is null/undefined (then nothing is stored). O(log n) plus the cost of fn.

K

(key) => V | null | undefined

V | null

the existing non-null value, the newly stored value, or null

NullPointerException if fn is null, or key is null under natural ordering and must be compared or stored

ConcurrentModificationException if fn structurally modifies this map

NavigableMap.computeIfAbsent

AbstractMap.computeIfAbsent


computeIfPresent(key, fn): V | null

Defined in: src/map/TreeMap.ts:861

If key has a non-null value, replaces it with fn(key, value); a null/undefined result removes the mapping. O(log n) plus the cost of fn.

K

(key, value) => V | null | undefined

V | null

the new value, or null if the key was absent or has been removed

NullPointerException if fn is null, or key is null or undefined under natural ordering

ConcurrentModificationException if fn structurally modifies this map

NavigableMap.computeIfPresent

AbstractMap.computeIfPresent


containsKey(key): boolean

Defined in: src/map/TreeMap.ts:724

Returns true if this map has a mapping for key, even if it maps to null. O(log n); keys are matched by the ordering, not equals().

unknown

boolean

NullPointerException if key is null or undefined under natural ordering

ClassCastException if key cannot be compared with the keys in this map

NavigableMap.containsKey

AbstractMap.containsKey


containsValue(value): boolean

Defined in: src/map/TreeMap.ts:729

Returns true if one or more keys map to a value equal to value. O(n), scanning values in key order.

unknown

boolean

NavigableMap.containsValue

AbstractMap.containsValue


descendingKeySet(): NavigableSet<K>

Defined in: src/map/TreeMap.ts:981

Returns a live, descending view of the keys. Removing from it removes the mapping; add throws UnsupportedOperationException.

NavigableSet<K>

NavigableMap.descendingKeySet


descendingMap(): NavigableMap<K, V>

Defined in: src/map/TreeMap.ts:989

Returns a live view of this map in descending key order: changes through either side are visible in the other. Created once and cached.

NavigableMap<K, V>

NavigableMap.descendingMap


entrySet(): SequencedSet<MapEntry<K, V>>

Defined in: src/map/TreeMap.ts:949

Returns a live view of the mappings in ascending key order. Entries from its iterator write through with setValue; removing from it removes the mapping, and it does not support add (UnsupportedOperationException).

SequencedSet<MapEntry<K, V>>

NavigableMap.entrySet

AbstractMap.entrySet


equals(o): boolean

Defined in: src/abstract/AbstractMap.ts:158

Returns true if o is a Map with the same mappings (any Map implementation). A ClassCastException or NullPointerException from o’s lookups counts as “not equal”.

unknown

boolean

NavigableMap.equals

AbstractMap.equals


firstEntry(): MapEntry<K, V> | null

Defined in: src/map/TreeMap.ts:1100

Returns the entry with the lowest key as an immutable snapshot, or null if this map is empty. O(log n).

MapEntry<K, V> | null

NavigableMap.firstEntry


firstKey(): K

Defined in: src/map/TreeMap.ts:1087

Returns the lowest key. O(log n).

K

NoSuchElementException if this map is empty

NavigableMap.firstKey


floorEntry(key): MapEntry<K, V> | null

Defined in: src/map/TreeMap.ts:1170

Returns the entry with the greatest key less than or equal to key (an immutable snapshot), or null. O(log n).

K

MapEntry<K, V> | null

NullPointerException if key is null or undefined under natural ordering and this map is not empty

NavigableMap.floorEntry


floorKey(key): K | null

Defined in: src/map/TreeMap.ts:1178

Returns the greatest key less than or equal to key, or null. O(log n).

K

K | null

NullPointerException if key is null or undefined under natural ordering and this map is not empty

NavigableMap.floorKey


forEach(action): void

Defined in: src/map/TreeMap.ts:809

Calls action for each mapping, in ascending key order.

(key, value) => void

void

NullPointerException if action is null

ConcurrentModificationException if action structurally modifies this map

NavigableMap.forEach

AbstractMap.forEach


get(key): V | null

Defined in: src/map/TreeMap.ts:713

Returns the value mapped to key, or null if there is none (or the key maps to null). O(log n).

unknown

V | null

NullPointerException if key is null or undefined under natural ordering

ClassCastException if key cannot be compared with the keys in this map

NavigableMap.get

AbstractMap.get


getOrDefault(key, defaultValue): V

Defined in: src/map/TreeMap.ts:739

Returns the value mapped to key, or defaultValue if there is no mapping. A key mapped to null returns null. O(log n).

unknown

V

V

NullPointerException if key is null or undefined under natural ordering

NavigableMap.getOrDefault

AbstractMap.getOrDefault

getOrDefault(key, defaultValue): V | null

Defined in: src/map/TreeMap.ts:745

Returns the value mapped to key, or defaultValue (here possibly null) if there is no mapping. O(log n).

unknown

V | null

V | null

NullPointerException if key is null or undefined under natural ordering

NavigableMap.getOrDefault

AbstractMap.getOrDefault


hashCode(): number

Defined in: src/abstract/AbstractMap.ts:179

Returns the sum of hashCode(key) ^ hashCode(value) over the mappings, so equal maps hash the same.

number

NavigableMap.hashCode

AbstractMap.hashCode


headMap(toKey): SortedMap<K, V>

Defined in: src/map/TreeMap.ts:1028

Returns a live view of the mappings whose keys are strictly less than toKey; same as headMap(toKey, false). O(1) to create, O(log n) per operation; putting a key outside the range through the view throws IllegalArgumentException.

K

SortedMap<K, V>

NullPointerException if toKey is null or undefined under natural ordering

NavigableMap.headMap

headMap(toKey, inclusive): NavigableMap<K, V>

Defined in: src/map/TreeMap.ts:1034

Returns a live view of the mappings whose keys are less than (or, if inclusive, equal to) toKey. O(1) to create.

K

boolean

NavigableMap<K, V>

NullPointerException if toKey is null or undefined under natural ordering

NavigableMap.headMap


higherEntry(key): MapEntry<K, V> | null

Defined in: src/map/TreeMap.ts:1202

Returns the entry with the least key strictly greater than key (an immutable snapshot), or null. O(log n).

K

MapEntry<K, V> | null

NullPointerException if key is null or undefined under natural ordering and this map is not empty

NavigableMap.higherEntry


higherKey(key): K | null

Defined in: src/map/TreeMap.ts:1210

Returns the least key strictly greater than key, or null. O(log n).

K

K | null

NullPointerException if key is null or undefined under natural ordering and this map is not empty

NavigableMap.higherKey


isEmpty(): boolean

Defined in: src/map/TreeMap.ts:700

Returns true if this map has no mappings. O(1).

boolean

NavigableMap.isEmpty

AbstractMap.isEmpty


keySet(): NavigableSet<K>

Defined in: src/map/TreeMap.ts:957

Returns a live, ascending view of the keys; same as navigableKeySet(). Removing from it removes the mapping; it does not support add (UnsupportedOperationException).

NavigableSet<K>

NavigableMap.keySet

AbstractMap.keySet


lastEntry(): MapEntry<K, V> | null

Defined in: src/map/TreeMap.ts:1105

Returns the entry with the highest key as an immutable snapshot, or null if this map is empty. O(1).

MapEntry<K, V> | null

NavigableMap.lastEntry


lastKey(): K

Defined in: src/map/TreeMap.ts:1095

Returns the highest key. O(1): the maximum is cached.

K

NoSuchElementException if this map is empty

NavigableMap.lastKey


lowerEntry(key): MapEntry<K, V> | null

Defined in: src/map/TreeMap.ts:1154

Returns the entry with the greatest key strictly less than key (an immutable snapshot), or null. O(log n).

K

MapEntry<K, V> | null

NullPointerException if key is null or undefined under natural ordering and this map is not empty

NavigableMap.lowerEntry


lowerKey(key): K | null

Defined in: src/map/TreeMap.ts:1162

Returns the greatest key strictly less than key, or null. O(log n).

K

K | null

NullPointerException if key is null or undefined under natural ordering and this map is not empty

NavigableMap.lowerKey


merge(key, value, fn): V | null

Defined in: src/map/TreeMap.ts:896

If key is absent or mapped to null, maps it to value; otherwise stores fn(oldValue, value), and a null/undefined result removes the mapping. O(log n) plus the cost of fn.

K

V

(oldValue, value) => V | null | undefined

V | null

the new value, or null if the mapping was removed

NullPointerException if value or fn is null, or key is null or undefined under natural ordering

ConcurrentModificationException if fn structurally modifies this map

NavigableMap.merge

AbstractMap.merge


navigableKeySet(): NavigableSet<K>

Defined in: src/map/TreeMap.ts:973

Returns a live, ascending view of the keys. Removing from it removes the mapping; add throws UnsupportedOperationException.

NavigableSet<K>

NavigableMap.navigableKeySet


pollFirstEntry(): MapEntry<K, V> | null

Defined in: src/map/TreeMap.ts:1113

Removes and returns the entry with the lowest key as an immutable snapshot, or null if this map is empty. O(log n).

MapEntry<K, V> | null

NavigableMap.pollFirstEntry


pollLastEntry(): MapEntry<K, V> | null

Defined in: src/map/TreeMap.ts:1125

Removes and returns the entry with the highest key as an immutable snapshot, or null if this map is empty. O(log n).

MapEntry<K, V> | null

NavigableMap.pollLastEntry


put(key, value): V | null

Defined in: src/map/TreeMap.ts:759

Maps key to value, replacing any previous value. O(log n); O(1) to find the slot for a key above the current maximum. Values may be null.

K

V

V | null

the previous value, or null if there was no mapping

NullPointerException if key is null or undefined under natural ordering

ClassCastException if key cannot be compared with the keys in this map

NavigableMap.put

AbstractMap.put


putAll(m): void

Defined in: src/abstract/AbstractMap.ts:119

Calls put for each mapping of a snapshot of m’s entries.

Map<K, V>

void

NullPointerException if m is null

UnsupportedOperationException if put is not supported and m is not empty

NavigableMap.putAll

AbstractMap.putAll


putFirst(_key, _value): V | null

Defined in: src/map/TreeMap.ts:1137

Not supported: the key order decides positions.

K

V

V | null

UnsupportedOperationException always

NavigableMap.putFirst


putIfAbsent(key, value): V | null

Defined in: src/map/TreeMap.ts:768

Maps key to value if it is absent or mapped to null. O(log n).

K

V

V | null

the current value if it was non-null (and nothing changed), otherwise null

NullPointerException if key is null or undefined under natural ordering

NavigableMap.putIfAbsent

AbstractMap.putIfAbsent


putLast(_key, _value): V | null

Defined in: src/map/TreeMap.ts:1145

Not supported: the key order decides positions.

K

V

V | null

UnsupportedOperationException always

NavigableMap.putLast


remove(key): V | null

Defined in: src/map/TreeMap.ts:777

Removes the mapping for key, if present. O(log n).

unknown

V | null

the removed value, or null if there was no mapping

NullPointerException if key is null or undefined under natural ordering

NavigableMap.remove

AbstractMap.remove

remove(key, value): boolean

Defined in: src/map/TreeMap.ts:783

Removes the mapping for key only if it is currently mapped to a value equal to value. O(log n).

unknown

unknown

boolean

true if the mapping was removed

NullPointerException if key is null or undefined under natural ordering

NavigableMap.remove

AbstractMap.remove


replace(key, value): V | null

Defined in: src/abstract/AbstractMap.ts:250

Puts value only if key already has a mapping; returns the previous value, or null if there was no mapping.

K

V

V | null

NavigableMap.replace

AbstractMap.replace

replace(key, oldValue, newValue): boolean

Defined in: src/abstract/AbstractMap.ts:252

Puts newValue only if key is mapped to a value equal to oldValue; returns true if replaced.

K

V

V

boolean

NavigableMap.replace

AbstractMap.replace


replaceAll(fn): void

Defined in: src/map/TreeMap.ts:823

Replaces each value with fn(key, value), in ascending key order. This is not a structural change.

(key, value) => V

void

NullPointerException if fn is null

ConcurrentModificationException if fn structurally modifies this map

NavigableMap.replaceAll

AbstractMap.replaceAll


reversed(): NavigableMap<K, V>

Defined in: src/map/TreeMap.ts:994

Returns a live descending view; same as descendingMap().

NavigableMap<K, V>

NavigableMap.reversed


sequencedEntrySet(): SequencedSet<MapEntry<K, V>>

Defined in: src/map/TreeMap.ts:1018

Returns a live, sequenced view of the mappings in key order: the entrySet() view, except that add and addAll throw UnsupportedOperationException.

SequencedSet<MapEntry<K, V>>

NavigableMap.sequencedEntrySet


sequencedKeySet(): NavigableSet<K>

Defined in: src/map/TreeMap.ts:1002

Returns a live, sequenced view of the keys in ascending order: the navigableKeySet() view, except that add and addAll throw UnsupportedOperationException.

NavigableSet<K>

NavigableMap.sequencedKeySet


sequencedValues(): SequencedCollection<V>

Defined in: src/map/TreeMap.ts:1010

Returns a live, sequenced view of the values in key order: the values() view, except that add and addAll throw UnsupportedOperationException.

SequencedCollection<V>

NavigableMap.sequencedValues


size(): number

Defined in: src/map/TreeMap.ts:695

Returns the number of mappings. O(1).

number

NavigableMap.size

AbstractMap.size


subMap(fromKey, toKey): SortedMap<K, V>

Defined in: src/map/TreeMap.ts:1062

Returns a live view of the mappings with keys from fromKey (inclusive) to toKey (exclusive); same as subMap(fromKey, true, toKey, false). O(1) to create.

K

K

SortedMap<K, V>

IllegalArgumentException if fromKey is greater than toKey

NullPointerException if either bound is null or undefined under natural ordering

NavigableMap.subMap

subMap(fromKey, fromInclusive, toKey, toInclusive): NavigableMap<K, V>

Defined in: src/map/TreeMap.ts:1069

Returns a live view of the mappings with keys between fromKey and toKey, each end included when its flag is set. O(1) to create.

K

boolean

K

boolean

NavigableMap<K, V>

IllegalArgumentException if fromKey is greater than toKey

NullPointerException if either bound is null or undefined under natural ordering

NavigableMap.subMap


tailMap(fromKey): SortedMap<K, V>

Defined in: src/map/TreeMap.ts:1045

Returns a live view of the mappings whose keys are greater than or equal to fromKey; same as tailMap(fromKey, true). O(1) to create, O(log n) per operation; putting a key outside the range through the view throws IllegalArgumentException.

K

SortedMap<K, V>

NullPointerException if fromKey is null or undefined under natural ordering

NavigableMap.tailMap

tailMap(fromKey, inclusive): NavigableMap<K, V>

Defined in: src/map/TreeMap.ts:1051

Returns a live view of the mappings whose keys are greater than (or, if inclusive, equal to) fromKey. O(1) to create.

K

boolean

NavigableMap<K, V>

NullPointerException if fromKey is null or undefined under natural ordering

NavigableMap.tailMap


toString(): string

Defined in: src/abstract/AbstractMap.ts:191

Returns {k1=v1, k2=v2} in entry-set order; a key or value that is this map prints as (this Map).

string

NavigableMap.toString

AbstractMap.toString


values(): SequencedCollection<V>

Defined in: src/map/TreeMap.ts:965

Returns a live view of the values in ascending key order. Removing from it removes the mapping; it does not support add (UnsupportedOperationException).

SequencedCollection<V>

NavigableMap.values

AbstractMap.values