javascript-collections-framework
    Preparing search index...

    Class LinkedHashSet<E>Sealed

    A set in insertion order that behaves like native Set — SameValueZero membership (NaN is one member, -0 is stored as 0), add returning the set and keeping a present member where it is, live iteration — and can also be read and changed at both ends: getFirst, getLast, removeFirst and removeLast, addFirst and addLast, which insert an absent member at that end or move the present one there, and a live reversed() view. Each takes O(1) amortised (average for those that look up or remove).

    A set without the key or equivalence option keeps its members in a native Set until the first call that needs linked order: addFirst, addLast of a member, getLast, removeLast, or any call through reversed(). That call costs O(n), once; this is why the ends, addFirst and addLast are amortised.

    The key and equivalence options work as they do for HashSet; the original values are stored and returned, never the keys. null and undefined are allowed.

    Iteration is live, as with native Set: members deleted before they are reached are skipped, members inserted at the end the loop is heading to are visited, members inserted at the end it started from are not, and nothing throws ConcurrentModificationError. A move acts as a delete followed by an insertion at the new end, so for (const x of set) set.addLast(x) never ends, exactly as for (const x of set) { set.delete(x); set.add(x) } never ends on a native Set. An open iterator or cursor keeps the members removed since its last step reachable until it advances or is dropped; release iterators that are no longer needed.

    Members are found by their key result or hash when they are stored. If that changes while a member is in the set, lookups, addFirst, addLast and delete give unspecified results for it; removeFirst, removeLast, a cursor's remove, removeIf and clear still remove it.

    Two sets are equal when they have the same size and each has every member of the other, in any order. A LinkedHashSet is not a HashSet (instanceof HashSet is false); both are sets. The class is not designed to be subclassed.

    const recent = new LinkedHashSet(['a', 'b', 'c'])
    recent.addLast('a') // LinkedHashSet(3) {'b', 'c', 'a'}
    recent.removeFirst() // 'b'
    [...recent.reversed()] // ['a', 'c']

    Type Parameters

    • E

    Implements

    Index
    • Creates a set holding each distinct element of elements once, in first-seen order; empty when elements is omitted, undefined or null. Options are never taken from a source set.

      Time: O(m) average for m elements, plus the key or equivalence calls.

      Type Parameters

      • E

      Parameters

      • Optionalelements: Iterable<E, any, any> | null

        The initial elements.

      • Optionaloptions: LinkedHashSetOptions<E> | null

        key or equivalence; null means none.

      Returns LinkedHashSet<E>

      TypeError when elements is neither iterable nor null/undefined, when options is not an object, when both key and equivalence are given, when key is not a function, or when equivalence lacks an equals or hash function.

      new LinkedHashSet([3, 1, 3]) // LinkedHashSet(2) {3, 1}
      new LinkedHashSet(points, { equivalence: valueEquivalence })
    • Creates an empty set with the given options.

      Time: O(1).

      Type Parameters

      • E

      Parameters

      Returns LinkedHashSet<E>

      TypeError when both key and equivalence are given, when key is not a function, or when equivalence lacks an equals or hash function.

      const byName = new LinkedHashSet<{ name: string }>({ key: (item) => item.name })
      
    • get "[COLLECTION_BRAND]"(): true

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

      Returns true

    • get "[LINKED_HASH_SET_BRAND]"(): true

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

      Returns true

    • get "[SEQUENCED_COLLECTION_BRAND]"(): true

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

      Returns true

    • get "[SET_COLLECTION_BRAND]"(): true

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

      Returns true

    • get "[toStringTag]"(): string

      'LinkedHashSet', for Object.prototype.toString. Time: O(1).

      Returns string

    • get revision(): number

      A number that changes on every insertion, removal or move and never on a read or on a call that changes nothing. Its exact values are unspecified. Time: O(1).

      Returns number

    • get size(): number

      The number of members. Time: O(1).

      Returns number

    • The value-equality protocol; delegates to equals. Time: as equals.

      Parameters

      • other: unknown

      Returns boolean

    • The value-equality protocol: an order-independent combination of the members' hashes, the same as an equal HashSet's with the same key function, the same equivalence, or neither. Values are unspecified. Time: O(n) plus the hash calls.

      Parameters

      Returns number

    • Node's inspect hook: LinkedHashSet(2) { 1, 2 } in console.log. Time: as toString.

      Parameters

      • depth: number | null
      • options: object
      • inspect: (value: unknown, options: object) => string

      Returns string

    • Returns an iterator over the members in order. It is live, as a native Set iterator is: members deleted before they are reached are skipped, members inserted at the end are visited, members inserted at the front are not, and a moved member counts as deleted and inserted at its new end. After clear() it continues with members added later; once done it stays done.

      Time: O(1) to create; O(1) per step, amortised over the steps and the changes made while it is open.

      Returns IterableIterator<E>

      A new live iterator.

      for (const tag of new LinkedHashSet(['a', 'b'])) console.log(tag)
      
    • Adds element at the end unless a member equals it, and returns the set, so calls chain. A member already present keeps its value and its place in the order.

      Time: O(1) average, plus one key or hash call and the equals calls.

      Parameters

      • element: E

        The element to add (-0 is stored as 0).

      Returns this

      The set.

      TypeError when key returns an object or a function, equivalence.equals returns a non-boolean or equivalence.hash a non-finite number.

      ConcurrentModificationError when a key, equals or hash function inserts, removes or moves members; the set keeps those changes and adds nothing itself.

      new LinkedHashSet([1, 2]).add(1).add(3) // LinkedHashSet(3) {1, 2, 3}
      
    • Adds every element of elements at the end, in order, after reading them all; members already present stay where they are. Elements added before an error stay.

      Time: O(m) average for m elements, plus the calls.

      Parameters

      • elements: Iterable<E>

        The elements to add.

      Returns boolean

      true when any element was added.

      TypeError when elements is not iterable, or a function returns an invalid result.

      ConcurrentModificationError when a key, equals or hash function inserts, removes or moves members.

      new LinkedHashSet([1]).addAll([1, 2]) // true
      
    • Inserts element at the front, or moves the member equal to it to the front (the stored member is kept, not replaced by element), and returns the set. Nothing changes when that member is already first.

      Time: O(1) amortised average, plus one key or hash call and the equals calls (the first call that needs linked order costs O(n), once).

      Parameters

      • element: E

        The element to insert or move (-0 is stored as 0).

      Returns this

      The set.

      TypeError when key, equals or hash returns an invalid result (as for add).

      ConcurrentModificationError when a key, equals or hash function inserts, removes or moves members; the set inserts and moves nothing itself.

      new LinkedHashSet([1, 2, 3]).addFirst(3) // LinkedHashSet(3) {3, 1, 2}
      
    • Inserts element at the end, or moves the member equal to it to the end (the stored member is kept, not replaced by element), and returns the set. Nothing changes when that member is already last.

      Time: O(1) amortised average, plus one key or hash call and the equals calls (the first call that needs linked order costs O(n), once).

      Parameters

      • element: E

        The element to insert or move (-0 is stored as 0).

      Returns this

      The set.

      TypeError when key, equals or hash returns an invalid result (as for add).

      ConcurrentModificationError when a key, equals or hash function inserts, removes or moves members; the set inserts and moves nothing itself.

      new LinkedHashSet([1, 2, 3]).addLast(1) // LinkedHashSet(3) {2, 3, 1}
      
    • Removes every member. An open iteration starts over before the first member, so members added at the end afterwards are visited, as with native Set.

      Time: O(1) with an equivalence; one native clear otherwise (at most O(n)).

      Returns void

      const set = new LinkedHashSet([1, 2])
      set.clear()
      set.size // 0
    • Returns a new LinkedHashSet with the same members, order and key or equivalence, independent of this one. Stored keys and hashes are copied; no user function is called.

      Time: O(n).

      Returns LinkedHashSet<E>

      The copy.

      const copy = set.copy()
      copy.addFirst(9) // set is unchanged
    • Returns a live cursor in order; remove() removes the member next() returned, from wherever it now stands, without calling key, equals or hash while that member is still stored. A member hasNext() has reached is returned even if it was deleted or moved meanwhile.

      Time: O(1) to create; next O(1) amortised over the steps and the changes made while it is open; remove O(1) average.

      Returns Cursor<E>

      A new cursor.

      const cursor = set.cursor()
      while (cursor.hasNext()) if (cursor.next() < 0) cursor.remove()
    • Removes the member equal to element.

      Time: O(1) average, plus one key or hash call and the equals calls.

      Parameters

      • element: E

        The value to remove.

      Returns boolean

      true when a member was removed.

      TypeError when key, equals or hash returns an invalid result (as for add).

      ConcurrentModificationError when a key, equals or hash function inserts, removes or moves members; the set removes nothing itself.

      new LinkedHashSet([1, 2]).delete(1) // true
      
    • Returns an iterator over [member, member] pairs in order, as native Set does.

      Time: O(1) to create; O(1) per step, amortised over the steps and the changes made while it is open.

      Returns IterableIterator<[E, E]>

      A new live iterator.

      [...new LinkedHashSet(['a']).entries()] // [['a', 'a']]
      
    • Returns whether other is a set of the same size which has every member of this set and whose every member this set has — whatever the order, and the same both ways round. Lists, queues, maps, arrays and native Set are never equal to it.

      Time: O(n) average, plus the has calls on both sets.

      Parameters

      • other: unknown

        Any value.

      Returns boolean

      true when other is an equal set.

      new LinkedHashSet([1, 2]).equals(new HashSet([2, 1])) // true
      
    • Calls action(member, member, set) for each member in order, with this set to thisArg, as native Set does. The loop is live, as [Symbol.iterator] describes.

      Time: O(n) plus the calls.

      Parameters

      • action: (element: E, same: E, set: SetCollection<E>) => void

        Called with each member twice and the set.

      • OptionalthisArg: unknown

        The this value for action.

      Returns void

      TypeError when action is not a function.

      new LinkedHashSet(['a', 'b']).forEach((tag) => console.log(tag))
      
    • Returns the first member.

      Time: O(1) amortised. Calls no key, equals or hash function.

      Returns E

      The first member.

      NoSuchElementError when the set is empty.

      new LinkedHashSet(['a', 'b']).getFirst() // 'a'
      
    • Returns the last member.

      Time: O(1) amortised (the first call that needs linked order costs O(n), once). Calls no key, equals or hash function.

      Returns E

      The last member.

      NoSuchElementError when the set is empty.

      new LinkedHashSet(['a', 'b']).getLast() // 'b'
      
    • Returns whether a member equals element.

      Time: O(1) average, plus one key or hash call and the equals calls.

      Parameters

      • element: E

        The value to look for.

      Returns boolean

      true when a member equals it.

      TypeError when key, equals or hash returns an invalid result (as for add).

      ConcurrentModificationError when a key, equals or hash function inserts, removes or moves members.

      new LinkedHashSet([Number.NaN]).has(Number.NaN) // true
      
    • Returns whether the set has no members.

      Time: O(1).

      Returns boolean

      size === 0.

      new LinkedHashSet().isEmpty() // true
      
    • Returns an iterator over the members in order, as values() does.

      Time: O(1) to create; O(1) per step, amortised over the steps and the changes made while it is open.

      Returns IterableIterator<E>

      A new live iterator.

      [...new LinkedHashSet(['a', 'b']).keys()] // ['a', 'b']
      
    • Removes every member equal, under this set's equality, to an element of elements. The elements are read first and looked up through a temporary index built with this set's key or equivalence; an error while deciding removes nothing.

      Time: O(n + m) average, plus one key or hash call per element and the equals calls.

      Parameters

      • elements: Iterable<E>

        The elements to remove.

      Returns boolean

      true when any member was removed.

      TypeError when elements is not iterable, or a function returns an invalid result.

      ConcurrentModificationError when a key, equals or hash function inserts, removes or moves members.

      new LinkedHashSet([1, 2, 3]).removeAll([2, 9]) // true; LinkedHashSet(2) {1, 3}
      
    • Removes and returns the first member.

      Time: O(1) amortised average. Calls no key, equals or hash function.

      Returns E

      The removed member.

      NoSuchElementError when the set is empty; nothing changes.

      new LinkedHashSet(['a', 'b']).removeFirst() // 'a'
      
    • Removes every member for which predicate returns a truthy value. Every member is decided first, in order; if predicate throws, or inserts, removes or moves members, nothing is removed.

      Time: O(n) plus the calls.

      Parameters

      • predicate: (element: E) => unknown

        Called with each member.

      Returns boolean

      true when any member was removed.

      TypeError when predicate is not a function.

      ConcurrentModificationError when predicate inserts, removes or moves members.

      new LinkedHashSet([1, 2, 3, 4]).removeIf((n) => n % 2 === 0) // true; LinkedHashSet(2) {1, 3}
      
    • Removes and returns the last member.

      Time: O(1) amortised average (the first call that needs linked order costs O(n), once). Calls no key, equals or hash function.

      Returns E

      The removed member.

      NoSuchElementError when the set is empty; nothing changes.

      new LinkedHashSet(['a', 'b']).removeLast() // 'b'
      
    • Removes every member not equal, under this set's equality, to an element of elements, as removeAll decides.

      Time: O(n + m) average, plus one key or hash call per element and the equals calls.

      Parameters

      • elements: Iterable<E>

        The elements to keep.

      Returns boolean

      true when any member was removed.

      TypeError when elements is not iterable, or a function returns an invalid result.

      ConcurrentModificationError when a key, equals or hash function inserts, removes or moves members.

      new LinkedHashSet([1, 2, 3]).retainAll([2]) // true; LinkedHashSet(1) {2}
      
    • Returns a live view of this set in the opposite order. Changes through either show in both; the view's getFirst, removeFirst and addFirst act on this set's last member or end, and its add inserts an absent member at the view's end, which is this set's front. Reversing the view returns this set.

      Time: O(1). Every call through the view needs linked order: the first such call on the set costs O(n), once, and the view's operations cost what the set's cost, amortised.

      Returns SequencedSet<E>

      The reversed view, printed as ReversedLinkedHashSet.

      const set = new LinkedHashSet([1, 2, 3])
      const newestFirst = set.reversed() // ReversedLinkedHashSet(3) {3, 2, 1}
      newestFirst.add(0) // set is now {0, 1, 2, 3}
    • Returns a new array of the members in order.

      Time: O(n).

      Returns E[]

      A new array.

      new LinkedHashSet([2, 1, 2]).toArray() // [2, 1]
      
    • Returns a new array of the members in order, so JSON.stringify writes the set as an array.

      Time: O(n).

      Returns E[]

      A new array.

      JSON.stringify(new LinkedHashSet([2, 1])) // '[2,1]'
      
    • Returns the class name, size and members in order, e.g. LinkedHashSet(2) {1, 'a'}.

      Time: O(length of the output) for flat contents.

      Returns string

      The text.

      String(new LinkedHashSet([1, 'a'])) // "LinkedHashSet(2) {1, 'a'}"
      
    • Returns an iterator over the members in order.

      Time: O(1) to create; O(1) per step, amortised over the steps and the changes made while it is open.

      Returns IterableIterator<E>

      A new live iterator.

      [...new LinkedHashSet(['a', 'b']).values()] // ['a', 'b']
      
    • Returns whether value is a LinkedHashSet, including one created by another copy of the library with the same major version. A reversed view is not one.

      Time: O(1).

      Parameters

      • value: unknown

        The value on the left of instanceof.

      Returns boolean

      true when value is a LinkedHashSet.

      new LinkedHashSet() instanceof LinkedHashSet // true
      
    • Returns a new set of the distinct elements of elements, in first-seen order, with the given options.

      Time: O(m) average for m elements, plus the key or equivalence calls.

      Type Parameters

      • T

      Parameters

      • elements: Iterable<T>

        The elements.

      • Optionaloptions: LinkedHashSetOptions<T> | null

        key or equivalence; null means none.

      Returns LinkedHashSet<T>

      A new set.

      TypeError when elements is not iterable (null and undefined included), or the options are invalid (as for the constructor).

      LinkedHashSet.from('banana') // LinkedHashSet(3) {'b', 'a', 'n'}