javascript-collections-framework
    Preparing search index...

    Class HashSet<E>Sealed

    A set that behaves like native Set — insertion order, SameValueZero membership (NaN is one member, -0 is stored as 0), add returning the set, live iteration — and adds value equality on request, structural equals, and the shared collection API (removeIf, removeAll, retainAll, cursors, toJSON).

    Give a key function to treat values with the same derived key as one member, or an equivalence (such as valueEquivalence) to decide membership by its equals and hash. 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 added during iteration are visited, and nothing throws ConcurrentModificationError. Changing a member's key or hash while it is in the set leaves lookups for it unspecified; it can still be removed by a cursor, removeIf or clear.

    Two sets are equal when they have the same size and each has every member of the other, in any order. The class is not designed to be subclassed.

    const tags = new HashSet(['a', 'b'])
    tags.add('a').add('c') // HashSet(3) {'a', 'b', 'c'}

    const users = new HashSet<{ id: number }>({ key: (user) => user.id })
    users.add({ id: 1 }).add({ id: 1 })
    users.size // 1

    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 (as new Set(null) is). A string is iterable, so new HashSet('ab') holds 'a' and 'b'.

      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: HashSetOptions<E> | null

        key or equivalence; null means none.

      Returns HashSet<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 HashSet([1, 2, 1]) // HashSet(2) {1, 2}
      new HashSet(points, { equivalence: valueEquivalence })
    • Creates an empty set with the given options.

      Time: O(1).

      Type Parameters

      • E

      Parameters

      Returns HashSet<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 HashSet<{ 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 "[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 "[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

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

      Returns string

    • get revision(): number

      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. 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 for equal sets that use 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: HashSet(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 insertion order. It is live, as a native Set iterator is: deleted members not yet reached are skipped, added members are visited, and clear() ends it.

      Time: O(1) to create; O(1) per step.

      Returns IterableIterator<E>

      A new live iterator.

      for (const tag of new HashSet(['a', 'b'])) console.log(tag)
      
    • Adds element 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 adds or removes members; the set keeps those changes and adds nothing itself.

      new HashSet<number>().add(1).add(1).add(2) // HashSet(2) {1, 2}
      
    • Adds every element of elements, in order, after reading them all. 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 adds or removes members.

      new HashSet([1]).addAll([1, 2]) // true
      
    • Removes every member. An open iteration ends, and members added 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 HashSet([1, 2])
      set.clear()
      set.size // 0
    • Returns a new HashSet 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 HashSet<E>

      The copy.

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

      Time: O(1) to create; next O(1); 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 adds or removes members; the set removes nothing itself.

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

      Time: O(1) to create; O(1) per step.

      Returns IterableIterator<[E, E]>

      A new live iterator.

      [...new HashSet(['a']).entries()] // [['a', 'a']]
      
    • 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 — whatever the insertion 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 HashSet([1, 2]).equals(new HashSet([2, 1])) // true
      new HashSet([1, 2]).equals(new Set([1, 2])) // false
    • Calls action(member, member, set) for each member in insertion order, with this set to thisArg, as native Set does. Members deleted before they are reached are skipped; members added during the loop are visited.

      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 HashSet(['a', 'b']).forEach((tag) => console.log(tag))
      
    • 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 adds or removes members.

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

      Time: O(1).

      Returns boolean

      size === 0.

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

      Time: O(1) to create; O(1) per step.

      Returns IterableIterator<E>

      A new live iterator.

      [...new HashSet(['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 adds or removes members.

      new HashSet([1, 2, 3]).removeAll([2, 9]) // true; HashSet(2) {1, 3}
      
    • 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) 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 adds or removes members.

      new HashSet([1, 2, 3, 4]).removeIf((n) => n % 2 === 0) // true; HashSet(2) {1, 3}
      
    • 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 adds or removes members.

      new HashSet([1, 2, 3]).retainAll([2]) // true; HashSet(1) {2}
      
    • Returns a new array of the members in insertion order.

      Time: O(n).

      Returns E[]

      A new array.

      new HashSet([2, 1, 2]).toArray() // [2, 1]
      
    • Returns a new array of the members in insertion order, so JSON.stringify writes the set as an array (a native Set is written as {}).

      Time: O(n).

      Returns E[]

      A new array.

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

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

      Returns string

      The text.

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

      Time: O(1) to create; O(1) per step.

      Returns IterableIterator<E>

      A new live iterator.

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

      Time: O(1).

      Parameters

      • value: unknown

        The value on the left of instanceof.

      Returns boolean

      true when value is a HashSet.

      new HashSet() instanceof HashSet // 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: HashSetOptions<T> | null

        key or equivalence; null means none.

      Returns HashSet<T>

      A new set.

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

      HashSet.from(['a', 'A'], { key: (s) => s.toLowerCase() }) // HashSet(1) {'a'}