SealedCreates a map in natural key order holding each mapping of entries; empty when entries is
omitted, undefined or null. Every item is checked before anything is stored; of several
equal keys the first key object is kept with the last value.
Time: O(m) with at most m − 1 comparisons (one when m is 1 in natural order) for m items
whose keys are strictly ascending; O(m log m) otherwise.
Creates an empty map with the given options.
Time: O(1).
The options; comparator is read once.
Creates a map holding each mapping of entries (empty when undefined or null), with keys
ordered by options.comparator, or by natural order when it is omitted.
Time: O(m) with at most m − 1 comparisons (one when m is 1 in natural order) for m items
whose keys are strictly ascending; O(m log m) otherwise.
Marks maps so isMapCollection recognises them across copies of the package. Its key is not
exported, so the getter is not called directly. Time: O(1).
Marks sequenced maps across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).
Marks sorted maps so isSortedMap recognises them across copies of the package. Its key is
not exported, so the getter is not called directly. Time: O(1).
'TreeMap', for Object.prototype.toString. Time: O(1).
Marks instances of this class so instanceof recognises them across copies of the package.
Its key is not exported, so the getter is not called directly. Time: O(1).
A number that changes when a mapping is added or removed or a value is written (even the same value), and never on a read or on a call that changes nothing. Its exact values are unspecified; views share it. Time: O(1).
The number of mappings. Time: O(1).
The value-equality protocol; delegates to equals. Time: as equals.
The value-equality protocol: an order-independent combination of the mappings' hashes through
hash. Equal maps get the same value when their comparators treat as equal only keys hash
hashes alike, as natural order does for primitives; then it is also the value of an equal
default HashMap. Values are unspecified. Time: O(n) plus the hash calls.
Node's inspect hook: TreeMap(1) { 1 => 'a' } in console.log. Time: as toString.
Returns the comparator given at construction, or undefined when the map uses natural order.
Time: O(1).
The comparator, or undefined.
Maps key to fn(currentValueOrUndefined, key, map), or removes the mapping when the result
is undefined, and returns the result. The key is checked before fn is called.
Time: as set, plus the call.
The result of fn.
When key is present, maps it to fn(value, key, map), or removes the mapping when the
result is undefined, and returns the result; otherwise returns undefined without calling
fn.
Time: as get, plus the call.
The result of fn, or undefined.
Returns whether some value is SameValueZero-equal to value.
Time: O(n).
The value to look for.
true when a mapping has that value.
Removes the mapping of key and returns whether there was one. In natural order, null,
undefined and keys that cannot be compared remove nothing.
Time: as get.
The key.
true when a mapping was removed.
Returns a live view of the mappings as entries, in key order. An entry is a member when its
key is present with an equal value; removing it removes the mapping; add, addFirst and
addLast throw UnsupportedOperationError. Its getFirst/getLast return the entries of the
least and greatest keys, removeFirst/removeLast remove those mappings (throwing
NoSuchElementError when empty), and reversed() is the entry view of reversed(). Each call
returns a new view.
Time: O(1); the view's ends O(log n).
A new view, tagged TreeMapEntrySet.
Returns whether other is a map of the same size in which every key of this map is present
with a value equal under equals, and which has no key this map lacks — whatever the order,
and the same both ways round, so a TreeMap equals a HashMap with the same mappings. Native
Map, sets, lists and plain objects are never equal to it.
Time: O(n log n), plus the lookups on other and the protocol methods.
Any value.
true when other is an equal map.
Calls action(value, key, map) for each mapping in ascending key order, with this set to
thisArg, as native Map does.
Time: O(n) plus the calls.
Called with each value, its key and the map.
OptionalthisArg: unknown
The this value for action.
Returns the value mapped to the key that compares equal to key, or undefined when there is
none. In natural order, null, undefined and keys that cannot be compared with the stored
keys give undefined. Use has or getOrDefault to tell a stored undefined from absence.
Time: O(log n), with at most 2⌈log2(n + 1)⌉ comparisons.
The key to look up.
The value, or undefined.
Returns the value of key when present; otherwise maps key to value and returns value,
as native Map.prototype.getOrInsert does.
Time: as set.
The mapped value.
Returns the value of key when present, without calling fn; otherwise calls fn(key), maps
key to its result and returns it, as native Map.prototype.getOrInsertComputed does. If
fn set the key itself, that mapping's value is overwritten in place and its key object kept;
fn may change the map without an error.
Time: as set, plus the call, plus one more search when fn added or removed mappings.
The mapped value.
Returns whether a key that compares equal to key is present. In natural order, null,
undefined and keys that cannot be compared give false.
Time: as get.
The key to look for.
true when the key is present.
Returns a live view of the mappings whose keys are less than to (or equal, with
inclusive: true). Changes through the view reach this map.
Time: O(log n) to create; as subMap for the view's operations.
The high end of the range.
Optionaloptions: BoundOptions | null
inclusive (default false); read once; undefined or null for the
default.
The view, tagged TreeSubMap.
Returns an iterator over the keys in ascending order.
Time: O(log n) to create; O(1) amortised per step.
A new iterator.
Returns a live view of the keys as a sorted set, in ascending order. It answers questions
about keys alone (getFirst, floor, ceiling, …); removing a key removes its mapping;
add, addFirst and addLast throw UnsupportedOperationError. Each call returns a new
view.
Time: O(1).
A new view, tagged TreeMapKeySet.
Maps key to value when it is absent (even when value is undefined); otherwise to
fn(old, value, key, map), removing the mapping when that result is undefined. Returns the
stored value, or undefined when the mapping was removed.
Time: as set, plus the call.
The stored value, or undefined.
Removes the mapping of the least key and returns its entry, or returns undefined when the
map is empty. The entry still reads its value; setValue on it throws IllegalStateError
until the key is set again. The comparator is not called.
Time: O(log n).
The entry of the removed mapping, or undefined.
Maps key to value, as set does, and returns the previous value, or undefined when the
key was absent.
Time: as set.
The previous value, or undefined.
Sets every mapping of entries in order, later ones replacing earlier ones for equal keys. The
whole argument is read and checked first — each item's shape and, in natural order, that every
key can be compared — so map.putAll(map) is safe and bad input stores nothing; mappings set
before a comparator error stay.
Time: O(m log(n + m)) for m items.
Always throws: a sorted map places each mapping by its key, so a mapping cannot be put at an
end. Use set. Nothing is checked or changed, and the comparator is not called.
Time: O(1).
Never returns.
Always throws: a sorted map places each mapping by its key, so a mapping cannot be put at an
end. Use set. Nothing is checked or changed, and the comparator is not called.
Time: O(1).
Never returns.
Replaces the value of key only when the key is present, and returns the previous value, or
undefined.
Time: as get.
The previous value, or undefined.
Replaces each value with fn(value, key, map), in key order. Values replaced before an error
stay.
Time: O(n) plus the calls.
Maps key to value and returns the map, so calls chain. When a key that compares equal is
present, its stored key object is kept and only the value is replaced. On an error nothing is
stored.
Time: O(log n), with at most 2⌈log2(n + 1)⌉ + 2 comparisons.
The map.
Returns a live view of the mappings whose keys run from from to to: from included and
to excluded unless the options say otherwise. Changes through the view reach this map;
setting a key outside the range through the view throws IllegalArgumentError, and looking
one up answers absent. The view's size counts its mappings.
Time: O(log n) to create; the view's operations cost O(log n), its size O(k + log n) for
k mappings in range.
The low end of the range.
The high end of the range.
Optionaloptions: SubSetOptions | null
fromInclusive (default true) and toInclusive (default false); read
once; undefined or null for the defaults.
The view, tagged TreeSubMap.
Returns a live view of the mappings whose keys are greater than or equal to from (or greater
only, with inclusive: false). Changes through the view reach this map.
Time: O(log n) to create; as subMap for the view's operations.
The low end of the range.
Optionaloptions: BoundOptions | null
inclusive (default true); read once; undefined or null for the
default.
The view, tagged TreeSubMap.
Returns the class name, size and mappings in ascending key order, e.g.
TreeMap(2) {1 => 'a', 2 => 'b'}; a map that contains itself prints as [Circular].
Time: O(length of the output) for flat contents.
The text.
Returns a live view of the values in key order, compared with SameValueZero. delete(v)
removes the mapping of the least key with that value; removeAll, retainAll and removeIf
every matching mapping; add, addFirst and addLast throw UnsupportedOperationError.
Its getFirst/getLast return the values of the least and greatest keys,
removeFirst/removeLast remove those mappings (throwing NoSuchElementError when empty),
and reversed() is the value view of reversed(). The view equals only itself. Each call
returns a new view.
Time: O(1); the view's ends O(log n).
A new view, tagged TreeMapValues.
Returns an iterator over the values in ascending key order.
Time: O(log n) to create; O(1) amortised per step.
A new iterator.
Static[hasReturns whether value is a TreeMap, including one created by another copy of the library
with the same major version. Its range and reversed views are not.
Time: O(1).
The value on the left of instanceof.
true when value is a TreeMap.
StaticfromReturns a new map of the mappings of entries, with keys ordered by options.comparator, or
by natural order when it is omitted. Every item is checked before anything is stored. The
comparator of a TreeMap given as entries is not used.
Time: O(m) with at most m − 1 comparisons (one when m is 1 in natural order) for m items
whose keys are strictly ascending; O(m log m) otherwise.
A new map.
A map kept in the order of its keys, stored as a balanced binary search tree:
get,set,has,deleteand the nearest-entry querieslowerEntry,floorEntry,ceilingEntryandhigherEntrycost O(log n). The order is the natural order of the keys, or thecomparatoroption.Key sameness is decided by the order alone: two keys are the same key when the comparator returns
0for them, whatever their identity, and setting such a key keeps the stored key object and replaces only the value. Iteration,keys,values,entries,forEach,toJSONandtoStringvisit the mappings in ascending key order.subMap,headMapandtailMapreturn live views of a key range, andreversed()a live view in descending order; changes through any view reach the map. Questions about keys alone go throughkeySet(), a live sorted set.In natural order,
nullandundefinedcannot be keys, and lookups (get,has,delete, …) answer "absent" for them and for keys of another kind, as a nativeMapdoes;-0is stored as0. Values may be anything,undefinedincluded. A comparator that throws or returnsNaNleaves the map as it was (putAllkeeps the mappings it set before the failure). A comparator that is not a consistent total order may make lookups give wrong answers, but never loses mappings or loops. Adding or removing mappings while iterating throwsConcurrentModificationError; writing a value does not.Two maps are equal when they have the same size and each maps every key of the other to an equal value, so a
TreeMapequals aHashMapwith the same mappings. The class is not designed to be subclassed.Example