SealedCreates a set in natural order holding the distinct elements of elements; empty when
elements is omitted, undefined or null.
Time: O(m) with at most m − 1 comparisons (one when m is 1 in natural
order) for m elements in strictly ascending order;
O(m log m) otherwise.
Optionalelements: Iterable<E, any, any> | null
The initial elements; of several equal ones the first is kept. The
comparator of a TreeSet given here is not used; pass it as an option, or call copy().
Creates an empty set with the given options.
Time: O(1).
The options; comparator is read once.
Creates a set holding the distinct elements of elements (empty when undefined or null),
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 elements in strictly ascending order;
O(m log m) otherwise.
The initial elements; of several equal ones the first is kept.
Optionaloptions: TreeSetOptions<E>
The options; comparator is read once.
Marks collections so isCollection recognises them across copies of the package. Its key is
not exported, so the getter is not called directly. Time: O(1).
Marks sequenced collections so isSequencedCollection recognises them across copies of the
package. Its key is not exported, so the getter is not called directly. Time: O(1).
Marks sets so isSetCollection recognises them across copies of the package. Its key is not
exported, so the getter is not called directly. Time: O(1).
Marks sorted sets so isSortedSet recognises them across copies of the package. Its key is
not exported, so the getter is not called directly. Time: O(1).
'TreeSet', 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 on every change to the members 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 members. Time: O(1).
The value-equality protocol; delegates to equals. Time: as equals.
The value-equality protocol: an order-independent combination of the members' hashes through
hash. Equal sets get the same value when their comparators treat as equal only values hash
hashes alike, as natural order does for primitives. Values are unspecified.
Time: O(n) plus the hash calls.
Node's inspect hook: TreeSet(2) { 1, 2 } in console.log. Time: as toString.
Returns an iterator over the members in ascending order.
Time: O(log n) to create; O(1) amortised per step.
A new iterator.
Adds element unless a member compares equal to it (that member is kept), and returns the
set, so calls chain. On an error nothing is added.
Time: O(log n), with at most 2⌈log2(n + 1)⌉ + 2 comparisons.
The element to add (in natural order, -0 is stored as 0).
The set.
Adds every element of elements, in order, after reading them all and, in natural order,
checking that none is null or undefined (if one is, nothing is added). If a comparison
fails part-way, the elements added before it stay.
Time: O(m log(n + m)) for m elements.
The elements to add.
true when any element was added.
Always throws: a sorted set places each member by its order. Use add.
Time: O(1).
Ignored.
Never returns.
Always throws: a sorted set places each member by its order. Use add.
Time: O(1).
Ignored.
Never returns.
Returns the comparator given at construction, or undefined when the set uses natural order.
Time: O(1).
The comparator, or undefined.
Removes the member that compares equal to element. In natural order, null, undefined and
values that cannot be compared with the members remove nothing.
Time: O(log n), with at most 2⌈log2(n + 1)⌉ comparisons.
The value to remove.
true when a member was removed.
Returns whether other is a set of the same size in which every member of this set is
present, and which has every member of this set — so a TreeSet equals a HashSet with the
same members, and the answer is the same both ways round. Lists, queues, arrays and native
Set are never equal to it.
Time: O(n log n), plus the has calls on other.
Any value.
true when other is an equal set.
Calls action(member, member, set) for each member in ascending order, with this set to
thisArg, as native Set does.
Time: O(n) plus the calls.
Called with each member twice and the set.
OptionalthisArg: unknown
The this value for action.
Returns the greatest member. The comparator is not called.
Time: O(log n).
The greatest member.
Returns whether a member compares equal to element. In natural order, null, undefined
and values that cannot be compared with the members give false.
Time: O(log n), with at most 2⌈log2(n + 1)⌉ comparisons.
The value to look for.
true when present.
Returns a live view of the members less than to (or equal, with inclusive: true). Changes
through the view reach this set.
Time: O(log n) to create; as subSet 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 TreeSubSet.
Returns an iterator over the members in ascending order, as values() does.
Time: O(log n) to create; O(1) amortised per step.
A new iterator.
Removes and returns the least member, or returns undefined when the set is empty. The
comparator is not called.
Time: O(log n).
The removed member, or undefined.
Removes and returns the greatest member, or returns undefined when the set is empty. The
comparator is not called.
Time: O(log n).
The removed member, or undefined.
Removes every member that compares equal to an element of elements. The elements are read
and each is looked up first; an error while looking up removes nothing.
Time: O((n + m) log n) for m elements.
The elements to remove.
true when any member was removed.
Removes and returns the least member. The comparator is not called.
Time: O(log n).
The removed member.
Removes every member for which predicate returns a truthy value. Every member is decided
first; if predicate throws, or adds or removes members, nothing is removed.
Time: O(n log n) plus the calls.
Called with each member, in order.
true when any member was removed.
Removes and returns the greatest member. The comparator is not called.
Time: O(log n).
The removed member.
Removes every member that compares equal to no element of elements, as removeAll decides.
Time: O((n + m) log n) for m elements.
The elements to keep.
true when any member was removed.
Returns a live view of the members from from to to: from included and to excluded
unless the options say otherwise. Changes through the view reach this set; adding a value
outside the range through the view throws IllegalArgumentError. The view's size counts
its members.
Time: O(log n) to create; the view's operations cost O(log n), its size O(k + log n) for
k members 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 TreeSubSet.
Returns a live view of the members greater than or equal to from (or greater only, with
inclusive: false). Changes through the view reach this set.
Time: O(log n) to create; as subSet 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 TreeSubSet.
Returns a new array of the members in ascending order, so JSON.stringify writes the set as
an array.
Time: O(n).
A new array.
Returns an iterator over the members in ascending order.
Time: O(log n) to create; O(1) amortised per step.
A new iterator.
Static[hasReturns whether value is a TreeSet, 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 TreeSet.
StaticfromReturns a new set of the distinct elements of elements, ordered by options.comparator, or
by natural order when it is omitted. The comparator of a TreeSet given as elements is not
used.
Time: O(m) with at most m − 1 comparisons (one when m is 1 in natural
order) for m elements in strictly ascending order;
O(m log m) otherwise.
The elements; of several equal ones the first is kept.
Optionaloptions: TreeSetOptions<T>
The options; comparator is read once.
A new set.
A set kept in sorted order, stored as a balanced binary search tree:
add,has,deleteand the nearest-match querieslower,floor,ceilingandhighercost O(log n). The order is the natural order of the members, or thecomparatoroption.Membership is decided by the order alone: two values are the same member when the comparator returns
0for them, whatever their identity. Iteration,toArray,toJSONandtoStringvisit the members in ascending order.subSet,headSetandtailSetreturn live views of a range, andreversed()a live view in descending order; changes through any view reach the set.In natural order,
nullandundefinedcannot be added, andhasanddeleteanswerfalsefor them and for values of another kind, as a nativeSetdoes;-0is stored as0. A comparator that throws or returnsNaNleaves the set as it was. A comparator that is not a consistent total order may make lookups give wrong answers, but never loses members or loops. Adding or removing members while iterating throwsConcurrentModificationError; use a cursor to remove while iterating.Two sets are equal when they have the same size and each has every member of the other. The class is not designed to be subclassed.
Example