# new TreeSet()
Example
let set = new TreeSet();
// add few values
set.add(1);
set.add(2);
// check whether key exists
let flag = set.has(1); // << true
// print all keys
for (let key of set) {
console.log(`key: ${key}`);
}
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
# add(key) → {void}
Adds a key to the set, unless the key already exists.
Parameters:
| Name | Type | Description |
|---|---|---|
key |
K
|
Key to be added |
void
Example
let set = new TreeSet();
set.add(1);
# backwards() → {IterableIterator}
ES6 reverse iterator for all keys in descending order.
reverse iterator for all elements
IterableIterator
Example
let set = new TreeSet([1, 2, 3]);
for (let key of set.backwards()) {
console.log(`key: ${key}`);
}
# begin() → {SetIterator}
Forward iterator to the first element
iterator pointing at element with the smallest key
Example
let set = new TreeSet();
...
for (let it = set.begin(); !it.equals(set.end()); it.next()) {
console.log(`key: ${it.key}`);
}
# clear()
Removes all key-value pairs.
Example
let set = new TreeSet([1, 2, 3]);
set.clear();
console.log(set.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
|
Key to be deleted |
void
Example
let set = new TreeSet([1, 2, 3]);
set.delete(2);
console.log(set.toString()); // {1,3}
# end() → {SetIterator}
Forward iterator to the element following the last element
iterator pointing at the node after the element with the largest key
Example
let set = new TreeSet();
...
for (let it = set.begin(); !it.equals(set.end()); it.next()) {
console.log(`key: ${it.key}`);
}
# entries() → {IterableIterator}
Forward ES6 iterator for all values in ascending order.
forward iterator for all elements
IterableIterator
Example
let set = new TreeSet([1, 2, 3]);
for (let key of set.entries()) {
console.log(`key: ${key}`);
}
# erase(iterator) → {void}
Removes value for the specified iterator.
Parameters:
| Name | Type | Description |
|---|---|---|
iterator |
SetIterator
|
pointing to the node to be removed |
void
Example
let set = new TreeSet([1,2,3]);
let it = set.find(2);
it.prev();
set.erase(it); // removes a node with key 1
console.log(set.toString()); // {2,3}
# find(key) → {SetIterator}
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
|
Key to search for |
iterator pointing at the found node
Example
let set = new TreeSet([1, 2, 3]);
...
let it = set.find(1);
if (!it.equals(set.end())) {
console.log(`Found key: ${it.key}`); // 1
}
# first() → {K}
Returns first element of the container, or undefined if container is empty
first element of the container, or undefined if container is empty
K
Example
let set = new TreeSet([1, 2, 3]);
let first = set.first(); // 1
# forEach(callback) → {void}
Iterates all values using a callback in ascending order. Note that ES6 specifies the order of key parameters in the callback differently from for-of loop.
Parameters:
| Name | Type | Description |
|---|---|---|
callback |
*
|
The same type of callback as used in |
void
Example
let set = new TreeSet([1, 2, 3]);
set.forEach(function(value, key, container) {
// value is the same as key
console.log(`key: ${key}, value: ${value}`);
});
# has(key) → {boolean}
A boolean indicator whether set contains the specified key.
Parameters:
| Name | Type | Description |
|---|---|---|
key |
K
|
a value of any type that can be compared with a key |
true if key exists in the container
boolean
Example
let set = new TreeSet([1, 2, 3]);
let b = set.get(3); // true
b = set.get(4); // false
# insertOrReplace(key) → {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 value |
indicates whether a node was added and provides iterator to it.
Example
let set = new TreeSet();
let res = set.insertOrReplace(1);
if (res.wasAdded) {
console.log(`Inserted ${res.iterator.key}`); // prints 1
}
res = set.insertOrReplace(1) // returns iterator to the previously added node
if (res.wasReplaced) {
console.log(`Replaced ${res.iterator.key}`); // prints 1
}
# insertUnique(key) → {InsertionResult}
Adds a key if it doesn't exist
Parameters:
| Name | Type | Description |
|---|---|---|
key |
K
|
Key to add |
indicates whether a node was added and provides iterator to it.
Example
let set = new TreeSet();
let res = set.insertUnique(1);
if (res.wasAdded) {
console.log(`Inserted ${res.iterator.key}`); // prints 1
}
res = set.insertUnique(1); // this step has no effect on the set
if (res.wasAdded) {
console.log(`Inserted ${res.iterator.key}`); // not executed
}
# keys() → {IterableIterator}
Forward ES6 iterator for all keys in ascending order.
forward iterator for all keys
IterableIterator
Example
// iterate all keys
let set = new TreeSet([1, 2, 3]);
for (let k of set.keys()) {
console.log(k); // 1, 2, 3
}
// iterate all keys in reverse order
let set = new TreeSet([1, 2, 3]);
for (let k of set.keys().backwards()) {
console.log(k); // 3, 2, 1
}
# last() → {K}
Returns last element of the container, or undefined if container is empty
last element of the container, or undefined if container is empty
K
Example
let set = new TreeSet([1, 2, 3]);
let last = set.last(); // 3
# lowerBound(key) → {SetIterator}
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 at found node
Example
let set = new TreeSet();
... // add key-value pairs., using numbers as keys
// iterate through all key-value pairs with keys between 0 and 50 inclusive
let from = set.lowerBound(0);
let to = set.upperBound(50);
let it = from;
while (!it.equals(to)) {
console.log(it.key);
it.next();
}
let set = new TreeSet();
... // 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(set.upperBound(50));
let to = new ReverseIterator(set.lowerBound(0));
let it = from;
while (!it.equals(to)) {
console.log(it.key);
it.next();
}
# rbegin() → {SetIterator}
Returns iterator to the first element for reverse iterator
iterator pointing to the node with the highest key
Example
let set = new TreeSet();
...
for (let it = set.rbegin(); !it.equals(set.rend()); it.next()) {
console.log(`key: ${it.key}`);
}
# rend() → {SetIterator}
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 set = new TreeSet();
...
for (let it = set.rbegin(); !it.equals(set.rend()); it.next()) {
console.log(`key: ${it.key}`);
}
# toString() → {string}
Serializes contents of the set in the form {key1,key2,...}
string representation of the set
string
# upperBound(key) → {SetIterator}
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 for |
iterator pointing at found node
Example
let set = new TreeSet();
... // add key-value pairs., using numbers as keys
// iterate through all key-value pairs with keys between 0 and 50 inclusive
let from = set.lowerBound(0);
let to = set.upperBound(50);
let it = from;
while (!it.equals(to)) {
console.log(it.key);
it.next();
}
let set = new TreeSet();
... // 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(set.upperBound(50));
let to = new ReverseIterator(set.lowerBound(0));
let it = from;
while (!it.equals(to)) {
console.log(it.key);
it.next();
}
# values() → {IterableIterator}
Forward ES6 iterator for all keys in ascending order. It is the same as keys() method
forward iterator for all values
IterableIterator
Example
// iterate all values
let set = new TreeSet([1, 2, 3]);
for (let v of set.values()) {
console.log(v); // '1', '2', '3'
}
// iterate all values in reverse order
let set = new TreeSet([1, 2, 3]);
for (let v of set.values().backwards()) {
console.log(v); // '3', '2', '1'
}