Options
All
  • Public
  • Public/Protected
  • All
Menu

Type parameters

  • T

    Definition (source):

    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.

    Use the Set variant of any of these Maps when: (source)

    • You just want to remember which keys you’ve seen.
    • There is no meaningful value to associate with your keys.
    • You just want a set.

    References

Hierarchy

  • Set<T>
    • HashSet

Implements

Index

Constructors

constructor

  • new HashSet<T>(values?: Iterable<T>): HashSet<T>
  • Type parameters

    • T

    Parameters

    • Optional values: Iterable<T>

    Returns HashSet<T>

Properties

Readonly [toStringTag]

[toStringTag]: string

Readonly size

size: number

Accessors

Static [species]

  • get [species](): SetConstructor
  • Returns SetConstructor

Methods

[iterator]

  • [iterator](): IterableIterator<T>
  • Iterates over values in the set.

    Returns IterableIterator<T>

add

  • Parameters

    • value: T

    Returns HashSet<T>

clear

  • clear(): void
  • Returns void

clone

  • Returns HashSet<T>

delete

  • delete(value: T): boolean
  • Parameters

    • value: T

    Returns boolean

difference

  • Visits the values representing the difference, i.e., the values that are in this but not in other.

    Example

    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]);
    

    Parameters

    Returns HashSet<T>

drain

  • drain(): T[]
  • Clears the set, returning all elements in an Iterable.

    Example

    const set = new HashSet([1, 2, 3, 4]);
    
    expect(set.drain()).toEqual([1, 2, 3, 4]);
    expect(set.isEmpty()).toBeTruthy();
    

    Returns T[]

drainFilter

  • drainFilter<F>(fn: F): T[]
  • Example

    ```ts const set = new HashSet([1, 2, 3, 4]);

    expect(set.drainFilter((v) => v % 2 === 0)).toEqual([2, 4]); expect(set.isEmpty()).toBeTruthy();

    
    

    Type parameters

    • F: (value: T, index: number) => boolean

    Parameters

    • fn: F

    Returns T[]

entries

  • entries(): IterableIterator<[T, T]>
  • Returns an iterable of [v,v] pairs for every value v in the set.

    Returns IterableIterator<[T, T]>

forEach

  • forEach(callbackfn: (value: T, value2: T, set: Set<T>) => void, thisArg?: any): void
  • Parameters

    • callbackfn: (value: T, value2: T, set: Set<T>) => void
        • (value: T, value2: T, set: Set<T>): void
        • Parameters

          • value: T
          • value2: T
          • set: Set<T>

          Returns void

    • Optional thisArg: any

    Returns void

has

  • has(value: T): boolean
  • Parameters

    • value: T

    Returns boolean

intersection

  • Visits the values representing the intersection, i.e., the values that are both in this and other.

    Example

    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]);
    

    Parameters

    Returns HashSet<T>

isDisjoint

  • isDisjoint(other: HashSet<T>): boolean
  • Returns true if this has no elements in common with other. This is equivalent to checking for an empty intersection.

    Example

    expect(new HashSet([2, 5, 1, 3]).isDisjoint(new HashSet([20]))).toBeTruthy();
    expect(new HashSet([2, 5, 1, 3]).isDisjoint(new HashSet([2]))).toBeFalsy();
    

    Parameters

    Returns boolean

isEmpty

  • isEmpty(): boolean
  • Returns boolean

isSubset

  • isSubset(other: HashSet<T>): boolean
  • Returns true if the set is a subset of another, i.e., other contains at least all the values in this.

    Parameters

    Returns boolean

isSuperset

  • isSuperset(other: HashSet<T>): boolean
  • Returns true if the set is a superset of another, i.e., this contains at least all the values in other.

    Example

    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();
    

    Q/A

    • Is the empty set a superset of itself?

    Yes. A ⊆ B ⟺ B ⊇ A so .... ∅ ⊆ ∅ ⟺ ∅ ⊇ ∅

    see

    More about it

    Parameters

    Returns boolean

keys

  • keys(): IterableIterator<T>
  • Despite its name, returns an iterable of the values in the set.

    Returns IterableIterator<T>

retain

  • retain<F>(fn: F): void
  • Retains only the elements specified by the predicate.

    In other words, remove all elements e such that fn(e) returns false.

    Example

    const set = new HashSet([1, 2, 3, 4]);
    
    set.retain((v) => v % 2 === 0);
    
    expect(set.toArray()).toEqual([2, 4]);
    expect(set.isEmpty()).toBeFalsy();
    

    Type parameters

    • F: (value: T) => boolean

    Parameters

    • fn: F

    Returns void

symmetricDifference

  • Visits the values representing the symmetric difference, i.e., the values that are in this or in other but not in both.

    Example

    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]);
    

    Parameters

    Returns HashSet<T>

toArray

  • toArray(): T[]
  • Returns T[]

toVec

  • Returns Vector<T>

union

  • Visits the values representing the union, i.e., all the values in this or other, without duplicates.

    Example

    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]);
    

    Parameters

    Returns HashSet<T>

values

  • values(): IterableIterator<T>
  • Returns an iterable of values in the set.

    Returns IterableIterator<T>

Generated using TypeDoc