Skip to content
Buy me a coffee

LinkedHashSet

Defined in: src/set/LinkedHashSet.ts:23

A hash Set that iterates in insertion order (re-adding an element keeps its place) and supports the SequencedSet operations. Elements are equal by equals()/hashCode(); null and undefined are allowed. add, remove and contains run in O(1) expected time, as do first/last access and removal; iterators are fail-fast. Backed by a LinkedHashMap.

Extends HashSet so instanceof HashSet holds, but HashSet keeps no map to reuse: every method is overridden to use a LinkedHashMap instead, and the inherited storage fields (shared empty ones) and modCount stay unused.

E

new LinkedHashSet<E>(): LinkedHashSet<E>

Defined in: src/set/LinkedHashSet.ts:29

Creates an empty set.

LinkedHashSet<E>

HashSet.constructor

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

Defined in: src/set/LinkedHashSet.ts:34

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

number

number

LinkedHashSet<E>

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

HashSet.constructor

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

Defined in: src/set/LinkedHashSet.ts:39

Creates a set with the elements of c, in c’s iteration order; duplicates are dropped.

Collection<E>

LinkedHashSet<E>

IllegalArgumentException if c is neither a number nor a collection

HashSet.constructor

modCount: number = 0

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

Structural modification count; iterators compare against it.

HashSet.modCount

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

HashSet.[SET_BRAND]

[INSPECT](): string

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

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

string

HashSet.[INSPECT]


[iterator](): IterableIterator<E>

Defined in: src/set/LinkedHashSet.ts:130

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

IterableIterator<E>

SequencedSet.[iterator]

HashSet.[iterator]


add(e): boolean

Defined in: src/set/LinkedHashSet.ts:99

Adds e if no equal element is present. O(1) expected. null and undefined are allowed. A new element goes to the end; re-adding an element keeps its place.

E

boolean

true if the set changed

SequencedSet.add

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

SequencedSet.addAll

HashSet.addAll


addFirst(e): void

Defined in: src/set/LinkedHashSet.ts:200

Adds e as the first element, or moves it to the front if already present. O(1) expected.

E

void

SequencedSet.addFirst


addLast(e): void

Defined in: src/set/LinkedHashSet.ts:205

Adds e as the last element, or moves it to the end if already present. O(1) expected.

E

void

SequencedSet.addLast


clear(): void

Defined in: src/set/LinkedHashSet.ts:112

Removes all elements.

void

SequencedSet.clear

HashSet.clear


clone(): LinkedHashSet<E>

Defined in: src/set/LinkedHashSet.ts:67

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

LinkedHashSet<E>

HashSet.clone


contains(o): boolean

Defined in: src/set/LinkedHashSet.ts:90

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

unknown

boolean

SequencedSet.contains

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

SequencedSet.containsAll

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

SequencedSet.equals

HashSet.equals


forEach(action): void

Defined in: src/set/LinkedHashSet.ts:139

Calls action for each element, in insertion order.

(e) => void

void

NullPointerException if action is null

ConcurrentModificationException if action structurally modifies this set

SequencedSet.forEach

HashSet.forEach


getFirst(): E

Defined in: src/set/LinkedHashSet.ts:171

Returns the first (eldest) element. O(1).

E

NoSuchElementException if this set is empty

SequencedSet.getFirst


getLast(): E

Defined in: src/set/LinkedHashSet.ts:179

Returns the last (youngest) element. O(1).

E

NoSuchElementException if this set is empty

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

SequencedSet.hashCode

HashSet.hashCode


isEmpty(): boolean

Defined in: src/set/LinkedHashSet.ts:82

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

boolean

SequencedSet.isEmpty

HashSet.isEmpty


iterator(): Iterator<E>

Defined in: src/set/LinkedHashSet.ts:117

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

Iterator<E>

SequencedSet.iterator

HashSet.iterator


parallelStream(): Stream<E>

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

Returns a stream over spliterator() flagged as parallel.

Stream<E>

SequencedSet.parallelStream

HashSet.parallelStream


remove(o): boolean

Defined in: src/set/LinkedHashSet.ts:107

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

unknown

boolean

true if an element was removed

SequencedSet.remove

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

SequencedSet.removeAll

HashSet.removeAll


removeFirst(): E

Defined in: src/set/LinkedHashSet.ts:187

Removes and returns the first element. O(1).

E

NoSuchElementException if this set is empty

SequencedSet.removeFirst


removeIf(filter): boolean

Defined in: src/set/LinkedHashSet.ts:161

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

SequencedSet.removeIf

HashSet.removeIf


removeLast(): E

Defined in: src/set/LinkedHashSet.ts:195

Removes and returns the last element. O(1).

E

NoSuchElementException if this set is empty

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

SequencedSet.retainAll

HashSet.retainAll


reversed(): SequencedSet<E>

Defined in: src/set/LinkedHashSet.ts:213

Returns a live reverse-ordered view: changes through either side are visible in the other. Writes go to this set: add of a new element appends it here, so it shows up first in the view.

SequencedSet<E>

SequencedSet.reversed


size(): number

Defined in: src/set/LinkedHashSet.ts:77

Returns the number of elements. O(1).

number

SequencedSet.size

HashSet.size


spliterator(): Spliterator<E>

Defined in: src/set/LinkedHashSet.ts:125

Returns a spliterator over the elements, used to build streams; fail-fast like the iterator. SIZED, SUBSIZED, DISTINCT and ORDERED; late-binding.

Spliterator<E>

SequencedSet.spliterator

HashSet.spliterator


stream(): Stream<E>

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

Returns a sequential stream over spliterator().

Stream<E>

SequencedSet.stream

HashSet.stream


toArray(): E[]

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

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

E[]

SequencedSet.toArray

HashSet.toArray

toArray<A>(generator): A

Defined in: src/set/LinkedHashSet.ts:149

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

A extends WritableArrayLike<E>

(length) => A

creates the target array for a given length

A

SequencedSet.toArray

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

SequencedSet.toString

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

HashSet.newHashSet


static newLinkedHashSet<E>(numElements): LinkedHashSet<E>

Defined in: src/set/LinkedHashSet.ts:54

Returns an empty set intended for numElements elements (capacity is only a hint, so this just validates it).

E

number

LinkedHashSet<E>

IllegalArgumentException if numElements is negative or NaN


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

Defined in: src/set/LinkedHashSet.ts:60

Returns a new set of the given elements in argument order; a repeated element keeps its first position.

E

…E[]

LinkedHashSet<E>

HashSet.of