A set is an abstract data type that can store unique values, without any particular order. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests a value for membership in a set.
Iterates over values in the set.
Visits the values representing the difference, i.e., the values that are in this
but not in other
.
const diff = new HashSet([2, 5, 1, 3]).difference(new HashSet([2]));
expect(diff.toArray()).toEqual([5, 1, 3]);
const diff = new HashSet([2, 5, 1, 3]).difference(new HashSet([20]));
expect(diff.toArray()).toEqual([2, 5, 1, 3]);
Clears the set, returning all elements in an Iterable
.
const set = new HashSet([1, 2, 3, 4]);
expect(set.drain()).toEqual([1, 2, 3, 4]);
expect(set.isEmpty()).toBeTruthy();
```ts const set = new HashSet([1, 2, 3, 4]);
expect(set.drainFilter((v) => v % 2 === 0)).toEqual([2, 4]); expect(set.isEmpty()).toBeTruthy();
Returns an iterable of [v,v] pairs for every value v
in the set.
Visits the values representing the intersection, i.e., the values that are both in this
and other
.
const intersection = new HashSet([1, 2, 3]).intersection(new HashSet([4, 5]));
expect(intersection.toArray()).toEqual([]);
const intersection = new HashSet([1, 2, 3]).intersection(new HashSet([4, 2, 3, 4]));
expect(intersection.toArray()).toEqual([2, 3]);
Returns true
if this
has no elements in common with other
. This is equivalent to checking for an empty intersection.
expect(new HashSet([2, 5, 1, 3]).isDisjoint(new HashSet([20]))).toBeTruthy();
expect(new HashSet([2, 5, 1, 3]).isDisjoint(new HashSet([2]))).toBeFalsy();
Returns true
if the set is a subset of another, i.e., other
contains at least all the values in this
.
Returns true
if the set is a superset of another, i.e., this
contains at least all the values in other
.
const isSuperset = new HashSet([1, 2, 3, 4]).isSuperset(new HashSet([15]));
expect(isSuperset).toBeFalsy();
const isSuperset = new HashSet([1, 2, 3, 4]).isSuperset(new HashSet([2, 3]));
expect(isSuperset).toBeTruthy();
const isSuperset = new HashSet([1, 2, 3, 4]).isSuperset(new HashSet([]));
expect(isSuperset).toBeTruthy();
Yes. A ⊆ B ⟺ B ⊇ A so .... ∅ ⊆ ∅ ⟺ ∅ ⊇ ∅
Despite its name, returns an iterable of the values in the set.
Retains only the elements specified by the predicate.
In other words, remove all elements e
such that fn(e)
returns false
.
const set = new HashSet([1, 2, 3, 4]);
set.retain((v) => v % 2 === 0);
expect(set.toArray()).toEqual([2, 4]);
expect(set.isEmpty()).toBeFalsy();
Visits the values representing the symmetric difference, i.e., the values that are in this
or in other
but not in both.
const diff = new HashSet([1, 2, 3]).symmetricDifference(new HashSet([1, 2, 3]));
expect(diff.toArray()).toEqual([]);
const diff = new HashSet([1, 2, 3, 5]).symmetricDifference(new HashSet([1, 2, 3, 4]));
expect(diff.toArray()).toEqual([5, 4]);
Visits the values representing the union, i.e., all the values in this
or other
, without duplicates.
const diff = new HashSet([2, 5, 1, 3]).union(new HashSet([20]));
expect(diff.toArray()).toEqual([2, 5, 1, 3, 20]);
const diff = new HashSet([2, 5, 1, 3]).union(new HashSet([2, 20]));
expect(diff.toArray()).toEqual([2, 5, 1, 3, 20]);
Returns an iterable of values in the set.
Generated using TypeDoc
JS Set
Rust HashSet
The Empty Set