Skip to content
Buy me a coffee

TreeSet

Defined in: src/set/TreeSet.ts:21

A sorted NavigableSet backed by a TreeMap (a red-black tree). Elements 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 elements are equal.

add, remove, contains and the navigation methods (floor, ceiling, higher, lower) run in O(log n); iteration is in ascending order. Under natural ordering null/undefined elements throw NullPointerException and elements of mixed types throw ClassCastException. Range and descending views are live; iterators are fail-fast. It shares its key-set machinery with TreeMap.navigableKeySet().

  • NavigableKeySet<E>

E

new TreeSet<E>(source?): TreeSet<E>

Defined in: src/set/TreeSet.ts:31

Creates an empty set, an empty set ordered by a comparator, or a copy of a collection. With no argument (or null) elements are ordered naturally. Copying a sorted set (a Set with a comparator() method) keeps the source’s comparator and order; any other collection is ordered naturally. The type system cannot tell the two source kinds apart, so the choice is made at run time.

Collection<E> | Comparator<E> | null

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

TreeSet<E>

IllegalArgumentException if source is neither a comparator nor a collection

NullPointerException / ClassCastException if a copied element cannot be compared

NavigableKeySet<E>.constructor

get [SET_BRAND](): true

Defined in: src/abstract/AbstractSet.ts:24

Runtime brand that marks this object as a Set (TypeScript interfaces do not exist at runtime); always true.

true

NavigableKeySet.[SET_BRAND]

[INSPECT](): string

Defined in: src/abstract/AbstractCollection.ts:195

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

string

NavigableKeySet.[INSPECT]


[iterator](): IterableIterator<E>

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

Returns a fail-fast iterator over the elements in ascending order.

IterableIterator<E>

NavigableSet.[iterator]

NavigableKeySet.[iterator]


add(e): boolean

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

Adds e if no element that compares equal is present. O(log n).

E

boolean

true if the set changed

UnsupportedOperationException on a key-set view of a map

NullPointerException if e is null or undefined under natural ordering

ClassCastException if e cannot be compared with the elements in this set

IllegalArgumentException if e is outside the range of a range view

NavigableSet.add

NavigableKeySet.add


addAll(c): boolean

Defined in: src/abstract/AbstractCollection.ts:96

Calls add for each element of a snapshot of c (so addAll(this) terminates); returns true if any call changed the collection.

Collection<E>

boolean

NullPointerException if c is null

UnsupportedOperationException if add is not supported and c is not empty

NavigableSet.addAll

NavigableKeySet.addAll


addFirst(_e): void

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

Not supported: the ordering decides positions.

E

void

UnsupportedOperationException always

NavigableSet.addFirst

NavigableKeySet.addFirst


addLast(_e): void

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

Not supported: the ordering decides positions.

E

void

UnsupportedOperationException always

NavigableSet.addLast

NavigableKeySet.addLast


ceiling(e): E | null

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

Returns the least element greater than or equal to e, or null if there is none. O(log n).

E

E | null

NullPointerException if e is null or undefined under natural ordering and this set is not empty

NavigableSet.ceiling

NavigableKeySet.ceiling


clear(): void

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

Removes all elements (for a range view, every element in its range).

void

NavigableSet.clear

NavigableKeySet.clear


clone(): TreeSet<E>

Defined in: src/set/TreeSet.ts:45

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

TreeSet<E>


comparator(): Comparator<E> | null

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

Returns the comparator that orders this set, or null when it uses natural ordering.

Comparator<E> | null

NavigableSet.comparator

NavigableKeySet.comparator


contains(o): boolean

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

Returns true if this set contains an element that compares equal to o. O(log n); the ordering, not equals(), decides equality.

unknown

boolean

NullPointerException if o is null or undefined under natural ordering

NavigableSet.contains

NavigableKeySet.contains


containsAll(c): boolean

Defined in: src/abstract/AbstractCollection.ts:83

Returns true if this collection contains every element of c (calls contains for each).

Collection<unknown>

boolean

NullPointerException if c is null

NavigableSet.containsAll

NavigableKeySet.containsAll


descendingIterator(): Iterator<E>

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

Returns a fail-fast iterator over the elements in descending order.

Iterator<E>

NavigableSet.descendingIterator

NavigableKeySet.descendingIterator


descendingSet(): NavigableSet<E>

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

Returns a live view of this set in descending order: changes through either side are visible in the other.

NavigableSet<E>

NavigableSet.descendingSet

NavigableKeySet.descendingSet


equals(o): boolean

Defined in: src/abstract/AbstractSet.ts:37

Returns true if o is a Set of the same size that this set containsAll of; any Set implementation compares. A ClassCastException or NullPointerException from containsAll counts as “not equal”.

unknown

boolean

NavigableSet.equals

NavigableKeySet.equals


first(): E

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

Returns the lowest element.

E

NoSuchElementException if this set is empty

NavigableSet.first

NavigableKeySet.first


floor(e): E | null

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

Returns the greatest element less than or equal to e, or null if there is none. O(log n).

E

E | null

NullPointerException if e is null or undefined under natural ordering and this set is not empty

NavigableSet.floor

NavigableKeySet.floor


forEach(action): void

Defined in: src/abstract/AbstractCollection.ts:157

Calls action for each element in iteration order.

(e) => void

void

NullPointerException if action is null

NavigableSet.forEach

NavigableKeySet.forEach


getFirst(): E

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

Returns the first (lowest) element; same as first().

E

NoSuchElementException if this set is empty

NavigableSet.getFirst

NavigableKeySet.getFirst


getLast(): E

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

Returns the last (highest) element; same as last().

E

NoSuchElementException if this set is empty

NavigableSet.getLast

NavigableKeySet.getLast


hashCode(): number

Defined in: src/abstract/AbstractSet.ts:81

Returns the sum of the element hashes (32-bit), so equal sets hash the same whatever their order.

number

NavigableSet.hashCode

NavigableKeySet.hashCode


headSet(toElement): SortedSet<E>

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

Returns a live view of the elements strictly less than toElement; same as headSet(toElement, false). O(1) to create; adding an element outside the range through the view throws IllegalArgumentException.

E

SortedSet<E>

NullPointerException if toElement is null or undefined under natural ordering

IllegalArgumentException if this set is itself a range view and toElement is outside its range

NavigableSet.headSet

NavigableKeySet.headSet

headSet(toElement, inclusive): NavigableSet<E>

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

Returns a live view of the elements less than (or, if inclusive, equal to) toElement. O(1) to create.

E

boolean

NavigableSet<E>

NullPointerException if toElement is null or undefined under natural ordering

IllegalArgumentException if this set is itself a range view and toElement is outside its range

NavigableSet.headSet

NavigableKeySet.headSet


higher(e): E | null

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

Returns the least element strictly greater than e, or null if there is none. O(log n).

E

E | null

NullPointerException if e is null or undefined under natural ordering and this set is not empty

NavigableSet.higher

NavigableKeySet.higher


isEmpty(): boolean

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

Returns true if this set has no elements. O(1) for a whole set; O(log n) for a range view.

boolean

NavigableSet.isEmpty

NavigableKeySet.isEmpty


iterator(): Iterator<E>

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

Returns a fail-fast iterator over the elements in ascending order; O(1) amortized per step. Its remove writes through.

Iterator<E>

NavigableSet.iterator

NavigableKeySet.iterator


last(): E

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

Returns the highest element.

E

NoSuchElementException if this set is empty

NavigableSet.last

NavigableKeySet.last


lower(e): E | null

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

Returns the greatest element strictly less than e, or null if there is none. O(log n).

E

E | null

NullPointerException if e is null or undefined under natural ordering and this set is not empty

NavigableSet.lower

NavigableKeySet.lower


parallelStream(): Stream<E>

Defined in: src/abstract/AbstractCollection.ts:180

Returns a stream over spliterator() flagged as parallel.

Stream<E>

NavigableSet.parallelStream

NavigableKeySet.parallelStream


pollFirst(): E | null

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

Removes and returns the lowest element, or null if this set is empty. O(log n).

E | null

NavigableSet.pollFirst

NavigableKeySet.pollFirst


pollLast(): E | null

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

Removes and returns the highest element, or null if this set is empty. O(log n).

E | null

NavigableSet.pollLast

NavigableKeySet.pollLast


remove(o): boolean

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

Removes the element that compares equal to o, if present. O(log n).

unknown

boolean

true if an element was removed

NullPointerException if o is null or undefined under natural ordering

NavigableSet.remove

NavigableKeySet.remove


removeAll(c): boolean

Defined in: src/abstract/AbstractSet.ts:56

Removes every element also in c. Walks the smaller side, sized by size(): when this set is larger, iterates c and calls this.remove(e) for each element (so this set’s equality decides, and remove(null) may throw NullPointerException); otherwise iterates this set, removing (iterator.remove, via removeIf) each element c.contains().

Collection<unknown>

boolean

NullPointerException if c is null, or this set contains null and c does not permit null

NavigableSet.removeAll

NavigableKeySet.removeAll


removeFirst(): E

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

Removes and returns the lowest element. O(log n).

E

NoSuchElementException if this set is empty

NavigableSet.removeFirst

NavigableKeySet.removeFirst


removeIf(filter): boolean

Defined in: src/abstract/AbstractCollection.ts:128

Removes every element for which filter returns true, through the iterator’s remove(); returns true if any was removed.

(e) => boolean

boolean

NullPointerException if filter is null

NavigableSet.removeIf

NavigableKeySet.removeIf


removeLast(): E

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

Removes and returns the highest element. O(log n).

E

NoSuchElementException if this set is empty

NavigableSet.removeLast

NavigableKeySet.removeLast


retainAll(c): boolean

Defined in: src/abstract/AbstractCollection.ts:118

Removes every element that c does not contain, through the iterator’s remove(); returns true if any was removed.

Collection<unknown>

boolean

NullPointerException if c is null

NavigableSet.retainAll

NavigableKeySet.retainAll


reversed(): NavigableSet<E>

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

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

NavigableSet<E>

NavigableSet.reversed

NavigableKeySet.reversed


size(): number

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

Returns the number of elements. O(1) for a whole set or its descending view; a range view counts its elements, O(k).

number

NavigableSet.size

NavigableKeySet.size


spliterator(): Spliterator<E>

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

Returns a spliterator over the elements, used to build streams; late-binding and fail-fast. TreeMap keys: SIZED (until split)|DISTINCT|SORTED|ORDERED, getComparator() = comparator(); full descending view: SIZED (until split)|DISTINCT|ORDERED; ascending range: DISTINCT|SORTED|ORDERED, descending range: DISTINCT|ORDERED, both of unknown size. Late-binding and fail-fast.

Spliterator<E>

NavigableSet.spliterator

NavigableKeySet.spliterator


stream(): Stream<E>

Defined in: src/abstract/AbstractCollection.ts:175

Returns a sequential stream over spliterator().

Stream<E>

NavigableSet.stream

NavigableKeySet.stream


subSet(fromElement, toElement): SortedSet<E>

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

Returns a live view of the elements from fromElement (inclusive) to toElement (exclusive); same as subSet(fromElement, true, toElement, false). O(1) to create.

E

E

SortedSet<E>

IllegalArgumentException if fromElement is greater than toElement, or either is outside this view’s range

NullPointerException if either bound is null or undefined under natural ordering

NavigableSet.subSet

NavigableKeySet.subSet

subSet(fromElement, fromInclusive, toElement, toInclusive): NavigableSet<E>

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

Returns a live view of the elements between fromElement and toElement, each end included when its flag is set. O(1) to create.

E

boolean

E

boolean

NavigableSet<E>

IllegalArgumentException if fromElement is greater than toElement, or either is outside this view’s range

NullPointerException if either bound is null or undefined under natural ordering

NavigableSet.subSet

NavigableKeySet.subSet


tailSet(fromElement): SortedSet<E>

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

Returns a live view of the elements greater than or equal to fromElement; same as tailSet(fromElement, true). O(1) to create; adding an element outside the range through the view throws IllegalArgumentException.

E

SortedSet<E>

NullPointerException if fromElement is null or undefined under natural ordering

IllegalArgumentException if this set is itself a range view and fromElement is outside its range

NavigableSet.tailSet

NavigableKeySet.tailSet

tailSet(fromElement, inclusive): NavigableSet<E>

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

Returns a live view of the elements greater than (or, if inclusive, equal to) fromElement. O(1) to create.

E

boolean

NavigableSet<E>

NullPointerException if fromElement is null or undefined under natural ordering

IllegalArgumentException if this set is itself a range view and fromElement is outside its range

NavigableSet.tailSet

NavigableKeySet.tailSet


toArray(): E[]

Defined in: src/abstract/AbstractCollection.ts:41

Returns a new array of the elements in iteration order.

E[]

NavigableSet.toArray

NavigableKeySet.toArray

toArray<A>(generator): A

Defined in: src/abstract/AbstractCollection.ts:48

Copies the elements in iteration order into generator(size()) and returns that array.

A extends WritableArrayLike<E>

(length) => A

receives the element count and returns a plain or typed array of at least that length

A

IndexOutOfBoundsException if the generated typed array is too short

NullPointerException if generator or its result is null

NavigableSet.toArray

NavigableKeySet.toArray


toString(): string

Defined in: src/abstract/AbstractCollection.ts:199

Returns [e1, e2, ...] in iteration order; an element that is this collection prints as (this Collection).

string

NavigableSet.toString

NavigableKeySet.toString


static of<E>(…elements): TreeSet<E>

Defined in: src/set/TreeSet.ts:38

Returns a new naturally ordered set of the given elements; duplicates are dropped.

E

…E[]

TreeSet<E>