# new TreeMap()
Example
let map = new TreeMap();
// add few values
map.set(1, 'a');
map.set(2, 'b');
// find a value by key
let v = map.get(1); // << 'a'
// print all key-value pairs
for (let [key, value] of map) {
console.log(`key: ${key}, value: ${value}`);
}
Members
# compareFunc
Sets custom comparison function if key values are not of primitive types. Callback is a 3-way comparison function accepts two key values (lhs, rhs). It is expected to return +1 if the value of rhs is greater than lhs -1 if the value of rhs is less than lhs 0 if values are the same
Methods
# backwards() → {IterableIterator}
ES6 reverse iterator for all key-value pairs in descending order of the keys.
reverse iterator for all elements in the container
IterableIterator
Example
let map = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
for (let [key,value] of map.backwards()) {
console.log(`key: ${key}, value: ${value}`);
}
# begin() → {MapIterator}
Forward iterator to the first element
forward iterator pointing at the first node
Example
let m = new TreeMap();
...
for (let it = m.begin(); !it.equals(m.end()); it.next()) {
console.log(`key: ${it.key}, value: ${it.value}`);
}
# clear()
Removes all key-value pairs.
Example
let map = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
map.clear();
console.log(map.size); // 0
# compareFunc(func)
Sets custom comparison function if key values are not of primitive types. Callback is a 3-way comparison function accepts two key values (lhs, rhs). It is expected to return +1 if the value of rhs is greater than lhs -1 if the value of rhs is less than lhs 0 if values are the same
Parameters:
| Name | Type | Description |
|---|---|---|
func |
compareFunctionType
|
# delete(key) → {void}
Removes key-value pair with the specified key if such entry exists. Does nothing otherwise.
Parameters:
| Name | Type | Description |
|---|---|---|
key |
K
|
Node's key |
void
Example
let map = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
map.delete(2);
console.log(map.toString()); // {1:A,3:C}
# end() → {MapIterator}
Forward iterator to the element following the last element
iterator to the node after the last element
Example
let m = new TreeMap();
...
for (let it = m.begin(); !it.equals(m.end()); it.next()) {
console.log(`key: ${it.key}, value: ${it.value}`);
}
# entries() → {IterableIterator}
Forward ES6 iterator for all key-value pairs in ascending order of the keys.
forward iterator for all nodes in the container
IterableIterator
Example
let map = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
for (let [key,value] of map.entries()) {
console.log(`key: ${key}, value: ${value}`);
}
# erase(iterator) → {void}
Removes key-value pair for the specified iterator.
Parameters:
| Name | Type | Description |
|---|---|---|
iterator |
MapIterator
|
iterator pointing to the node to be removed |
void
Example
let map = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
let it = map.find(2);
it.prev();
map.erase(it); // removes a node with key 1
console.log(map.toString()); // {2:B,3:C}
# find(key) → {MapIterator}
Finds an element with key equivalent to the specified one. If such key does not exist end() iterator is returned.
Parameters:
| Name | Type | Description |
|---|---|---|
key |
K
|
to check |
forward iterator to the node with the specified key
Example
let m = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
...
let it = m.find(1);
if (!it.equals(m.end())) {
console.log(`key: ${it.key}, value: ${it.value}`); // 1, 'A'
}
# first() → {Array|undefined}
Returns first key/value pair of the container, or undefined if container is empty
first key/value pair of the container, or undefined if container is empty
Array
|
undefined
Example
let m = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
let first = m.first();
if (first) {
let key = first[0]; // 1
let value = first[1]; // 'A'
}
# forEach(callback) → {void}
Iterates all key-value pairs using a callback in ascending order of the keys. Note that ES6 specifies the order of key value parameters in the callback differently from for-of loop.
Parameters:
| Name | Type | Description |
|---|---|---|
callback |
*
|
similar to forEach callbacks for standard JS maps |
void
Example
let map = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
map.forEach(function(value, key, container) {
console.log(`key: ${key}, value: ${value}`);
});
# get(key) → {V|undefined}
Finds value associated with the specified key. If specified key does not exist then undefined is returned.
Parameters:
| Name | Type | Description |
|---|---|---|
key |
K
|
a value of any type that can be compared with a key |
the value associated with the given key
V
|
undefined
Example
let map = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
let v = map.get(3); // 'C'
let v = map.get(4); // returns undefined
# has(key) → {boolean}
A boolean indicator whether map contains a key-value pair with the specified key
Parameters:
| Name | Type | Description |
|---|---|---|
key |
K
|
a value of any type that can be compared with a key |
returns true if given key exists in the container
boolean
Example
let map = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
let b = map.get(3); // true
# insertOrReplace(key, value) → {InsertionResult}
Adds key-value pair if such key does not exist in the map. Replaces value if such key exists
Parameters:
| Name | Type | Description |
|---|---|---|
key |
K
|
key to be added / replaced |
value |
V
|
new value |
indicates whether a node was added and provides iterator to it.
Example
let m = new TreeMap();
let res = m.insertOrReplace(1, 'A');
if (res.wasAdded) {
console.log(`Inserted ${res.iterator.value}`); // prints A
}
res = m.insertOrReplace(1, 'B') // replaces value on the existing node
if (res.wasReplaced) {
console.log(`Replaced ${res.iterator.key}`); // prints B
}
# insertUnique(key, value) → {InsertionResult}
Adds key-value pair if such key does not exist in the map
Parameters:
| Name | Type | Description |
|---|---|---|
key |
K
|
key value to insert |
value |
V
|
value to be associated with the key |
indicates whether a node was added and provides iterator to it.
Example
let m = new TreeMap();
let res = m.insertUnique(1, 'A');
if (res.wasAdded) {
console.log(`Inserted ${res.iterator.value}`); // prints A
}
res = m.insertUnique(1, 'B') // this step has no effect on the map
if (res.wasAdded) {
console.log(`Inserted ${res.iterator.key}`); // not executed
}
# keys() → {IterableIterator}
Forward ES6 iterator for all keys in ascending order of the keys.
forward iterator
IterableIterator
Example
// iterate all keys
let map = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
for (let k of map.keys()) {
console.log(k); // 1, 2, 3
}
// iterate all keys in reverse order
let map = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
for (let k of map.keys().backwards()) {
console.log(k); // 3, 2, 1
}
# last() → {Array|undefined}
Returns last key/value pair of the container, or undefined if container is empty
last key/value pair of the container, or undefined if container is empty
Array
|
undefined
Example
let m = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
let last = m.last();
if (last) {
let key = last[0]; // 3
let value = last[1]; // 'C'
}
# lowerBound(key) → {MapIterator}
Iterator pointing to the first element that is not less than specified key. If no such element is found, see end() iterator is returned.
Parameters:
| Name | Type | Description |
|---|---|---|
key |
K
|
key to search for |
iterator pointing to the found node
Example
let m = new TreeMap();
... // add key-value pairs., using numbers as keys
// iterate through all key-value pairs with keys between 0 and 50 inclusive
let from = m.lowerBound(0);
let to = m.upperBound(50);
let it = from;
while (!it.equals(to)) {
console.log(it.key);
it.next();
}
let m = new TreeMap();
... // add key-value pairs., using numbers as keys
// iterate through all key-value pairs with keys between 0 and 50 inclusive in reverse order
let from = new ReverseIterator(m.upperBound(50));
let to = new ReverseIterator(m.lowerBound(0));
let it = from;
while (!it.equals(to)) {
console.log(it.key);
it.next();
}
# rbegin() → {MapIterator}
Returns iterator to the first element for reverse iterator
iterator pointing to the node with the highest key
Example
let m = new TreeMap();
...
for (let it = m.rbegin(); !it.equals(m.rend()); it.next()) {
console.log(`key: ${it.key}, value: ${it.value}`);
}
# rend() → {MapIterator}
Returns end iterator for reverse iteration, e.g. pointing to a position after the last element
iterator pointing to the node preceding the node with the lowest key
Example
let m = new TreeMap();
...
for (let it = m.rbegin(); !it.equals(m.rend()); it.next()) {
console.log(`key: ${it.key}, value: ${it.value}`);
}
# set(key, value) → {void}
Adds or updates key-value pair to the map.
Parameters:
| Name | Type | Description |
|---|---|---|
key |
K
|
Key for the node to be added/updated |
value |
V
|
New value |
void
Example
let map = new TreeMap();
map.set(1, 'A');
# size() → {number}
Number of key-value pairs in the map.
number of elements in the container
number
# toString() → {string}
Serializes contents of the map in the form {key1:value1,key2:value2,...}
serialized representation of the tree
string
# upperBound(key) → {MapIterator}
Iterator pointing to the first element that is greater than key. If no such element is found end() iterator is returned.
Parameters:
| Name | Type | Description |
|---|---|---|
key |
K
|
Key to search |
iterator pointing to the found node
Example
let m = new TreeMap();
... // add key-value pairs., using numbers as keys
// iterate through all key-value pairs with keys between 0 and 50 inclusive
let from = m.lowerBound(0);
let to = m.upperBound(50);
let it = from;
while (!it.equals(to)) {
console.log(it.key);
it.next();
}
let m = new TreeMap();
... // add key-value pairs., using numbers as keys
// iterate through all key-value pairs with keys between 0 and 50 inclusive in reverse order
let from = new ReverseIterator(m.upperBound(50));
let to = new ReverseIterator(m.lowerBound(0));
let it = from;
while (!it.equals(to)) {
console.log(it.key);
it.next();
}
# values() → {IterableIterator}
Forward ES6 iterator for all values in ascending order of the keys.
forward iterator for all values
IterableIterator
Example
// iterate all values
let map = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
for (let v of map.values()) {
console.log(v); // 'A', 'B', 'C'
}
// iterate all values in reverse order
let map = new TreeMap([[1, 'A'], [2, 'B'], [3, 'C']]);
for (let v of map.values().backwards()) {
console.log(v); // 'C', 'B', 'A'
}