Skip to content
Buy me a coffee

HashSet

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

A hash table Set: the default set. Elements are equal by equals()/hashCode() (SameValueZero for primitives and objects without equals()); null and undefined are allowed; iteration order is unspecified. add, remove and contains run in O(1) expected time (O(k) when k elements share one hashCode()); iterators are fail-fast.

Same hybrid storage as HashMap: small non-negative integers as flags in a dense array, other SameValueZero elements in a native Set, elements with equals()/hashCode() and Dates in hashCode buckets (always arrays, so iterators can track them).

E

new HashSet<E>(): HashSet<E>

Defined in: src/set/HashSet.ts:36

Creates an empty set.

HashSet<E>

AbstractSet.constructor

new HashSet<E>(initialCapacity, loadFactor?): HashSet<E>

Defined in: src/set/HashSet.ts:41

Creates an empty set. The arguments are sizing hints only: they are validated but storage grows on demand.

number

number

HashSet<E>

IllegalArgumentException if initialCapacity is negative or NaN, or loadFactor is not positive

AbstractSet<E>.constructor

new HashSet<E>(c): HashSet<E>

Defined in: src/set/HashSet.ts:46

Creates a set with the elements of c; duplicates are dropped.

Collection<E>

HashSet<E>

IllegalArgumentException if c is neither a number nor a collection

AbstractSet<E>.constructor

modCount: number = 0

Defined in: src/set/HashSet.ts:33

Structural modification count; iterators compare against it.

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

AbstractSet.[SET_BRAND]

[INSPECT](): string

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

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

string

AbstractSet.[INSPECT]


[iterator](): IterableIterator<E>

Defined in: src/set/HashSet.ts:223

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

IterableIterator<E>

AbstractSet.[iterator]


add(e): boolean

Defined in: src/set/HashSet.ts:110

Adds e if no equal element is present. O(1) expected. null and undefined are allowed.

E

boolean

true if the set changed

AbstractSet.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

AbstractSet.addAll


clear(): void

Defined in: src/set/HashSet.ts:199

Removes all elements. Open iterators then fail with ConcurrentModificationException.

void

AbstractSet.clear


clone(): HashSet<E>

Defined in: src/set/HashSet.ts:71

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

HashSet<E>


contains(o): boolean

Defined in: src/set/HashSet.ts:96

Returns true if this set contains an element equal to o (by equals(), SameValueZero for primitives). O(1) expected.

unknown

boolean

AbstractSet.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

AbstractSet.containsAll


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

AbstractSet.equals


forEach(action): void

Defined in: src/set/HashSet.ts:232

Calls action for each element, in unspecified order.

(e) => void

void

NullPointerException if action is null

ConcurrentModificationException if action structurally modifies this set

AbstractSet.forEach


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

AbstractSet.hashCode


isEmpty(): boolean

Defined in: src/set/HashSet.ts:88

Returns true if this set has no elements. O(1).

boolean

AbstractSet.isEmpty


iterator(): Iterator<E>

Defined in: src/set/HashSet.ts:209

Returns a fail-fast iterator over the elements, in unspecified order; its remove is supported.

Iterator<E>

AbstractSet.iterator


parallelStream(): Stream<E>

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

Returns a stream over spliterator() flagged as parallel.

Stream<E>

AbstractSet.parallelStream


remove(o): boolean

Defined in: src/set/HashSet.ts:144

Removes the element equal to o, if present. O(1) expected.

unknown

boolean

true if an element was removed

AbstractSet.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

AbstractSet.removeAll


removeIf(filter): boolean

Defined in: src/set/HashSet.ts:280

Removes every element for which filter returns true, in one O(n) pass.

(e) => boolean

boolean

true if any element was removed

NullPointerException if filter is null

ConcurrentModificationException if filter structurally modifies this set

AbstractSet.removeIf


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

AbstractSet.retainAll


size(): number

Defined in: src/set/HashSet.ts:83

Returns the number of elements. O(1).

number

AbstractSet.size


spliterator(): Spliterator<E>

Defined in: src/set/HashSet.ts:217

Returns a spliterator over the elements, used to build streams; fail-fast like the iterator. SIZED and DISTINCT (SIZED until split); late-binding.

Spliterator<E>

AbstractSet.spliterator


stream(): Stream<E>

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

Returns a sequential stream over spliterator().

Stream<E>

AbstractSet.stream


toArray(): E[]

Defined in: src/set/HashSet.ts:257

Returns a new array with all elements, in unspecified order. Changing the array does not affect this set.

E[]

AbstractSet.toArray

toArray<A>(generator): A

Defined in: src/set/HashSet.ts:262

Fills an array created by generator(size()) with the elements, in unspecified order, and returns it.

A extends WritableArrayLike<E>

(length) => A

creates the target array for a given length

A

AbstractSet.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

AbstractSet.toString


static newHashSet<E>(numElements): HashSet<E>

Defined in: src/set/HashSet.ts:58

Returns an empty set intended for numElements elements. Capacity is only a hint here, so this just validates it.

E

number

HashSet<E>

IllegalArgumentException if numElements is negative or NaN


static of<E>(…xs): HashSet<E>

Defined in: src/set/HashSet.ts:64

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

E

…E[]

HashSet<E>