# Class Set<T>

GoJS® Diagramming Components
version 3.0.7
by Northwoods Software®

An unordered iterable collection that cannot contain two instances of the same value. In TypeScript it is a generic class that enforces at compile-time the type of elements that may be added to the Set.

An example usage:

      const set = new go.Set();  // In TypeScript: new go.Set<string>();
      set.add("orange");
      set.add("apple");
      set.add("orange");
      // now set.size === 2
      // and set.has("orange") === true
      // and set.has("banana") === false

You can iterate over the items in a Set:

      const it = aSet.iterator;
      while (it.next()) {
        . . . it.value . . .
      }

Or:

      aSet.each(val => {
          . . . val . . .
        });

For compatibility with versions of GoJS before version 3.0, the following synonyms are defined:

- remove(val): delete
- contains(val): has
- count: size

The constructor now takes an optional Iterable or Array argument that provides the initial elements for the new Set.

Note that GoJS iteration is quite different than ECMAScript iteration, so that functionality has not been made somewhat compatible. These collection classes were defined in GoJS before the ECMAScript collection classes were proposed.

#### Type Parameters

- #### T

#### Implements

- Iterable<T>

#####
Index

### Constructors

constructor

### Accessors

count
iterator
size

### Methods

[iterator]
add
addAll
all
any
clear
contains
containsAll
containsAny
copy
delete
each
first
forEach
has
remove
removeAll
retainAll
toArray
toList
values

## Constructors

### constructor

- new Set<T>(coll?: Iterable<T> | T[]): Set<T>

- There are two possible constructors:

  `new go.Set()`, for JavaScript

  `new go.Set<T>()` for TypeScript

  In TypeScript, the optional generic argument describes the type of values that this Set may hold.

  For example, the expression:

      // TypeScript:
      new go.Set<go.Point>()

  Creates a new Set that may only contain Points.

  #### Type Parameters

  - #### T

  #### Parameters

  - ##### `Optional` coll: Iterable<T> | T[]

    an optional collection of items to add.

  #### Returns Set<T>

## Accessors

### `Readonly` count
: number

- This read-only property is the number of elements in the Set.

### `Readonly` iterator
: Iterator<T>

- Gets an object that you can use for iterating over the Set. The value will be a member of the Set. Typical usage:

        const it = aSet.iterator;
        while (it.next()) {
          . . . " value: " + it.value . . .
        }

### `Readonly` size
: number

- This read-only property is the number of elements in the Set.

## Methods

### [iterator]

- [iterator](): IterableIterator<T>

- This class implements the JavaScript `Symbol.iterator`, and can be used with spread syntax or `for ... of` statements.

  #### Returns IterableIterator<T>

  `since`

  3.0

### add

- add(val: T): Set<T>

- Adds a given value to the Set, if not already present.

  Be careful not to call this method while iterating over the collection.

  #### Parameters

  - ##### val: T

    The value to add to the Set; must not be null.

  #### Returns Set<T>

  This modified Set.

### addAll

- addAll(coll: Iterable<T> | T[]): Set<T>

- Adds all of the values of a collection to this Set.

  Be careful not to call this method while iterating over the collection.

  #### Parameters

  - ##### coll: Iterable<T> | T[]

    the collection of items to add.

  #### Returns Set<T>

  This modified Set.

### `Virtual` all

- all(pred: ((a: T) => boolean)): boolean

- This is true if all invocations of the given predicate on items in the collection are true.

  Call the given predicate on each item in the collection. As soon as a call returns false, this returns false. Otherwise this returns true. For an empty collection this returns true.

  #### Parameters

  - ##### pred: ((a: T) => boolean)

    This function must not have any side-effects.

    - - (a: T): boolean

      - #### Parameters

        - ##### a: T

        #### Returns boolean

  True if all predicate calls are true; false otherwise.

### `Virtual` any

- any(pred: ((a: T) => boolean)): boolean

- This is true if any invocation of the given predicate on items in the collection is true.

  Call the given predicate on each item in the collection. As soon as a call returns true, this returns true. Otherwise this returns false. For an empty collection this returns false.

  #### Parameters

  - ##### pred: ((a: T) => boolean)

    This function must not have any side-effects.

    - - (a: T): boolean

      - #### Parameters

        - ##### a: T

        #### Returns boolean

  True if any predicate call is true; false otherwise.

### clear

- clear(): void

- Clears the Set. This sets the count to zero.

  Be careful not to call this method while iterating over the collection.

  #### Returns void

### contains

- contains(val: T): boolean

- Returns whether the given value is in this Set.

  #### Parameters

  - ##### val: T

    The value to check.

  #### Returns boolean

  Whether or not the value is contained within the Set.

### containsAll

- containsAll(coll: Iterable<T>): boolean

- Returns true if all of the values of a given collection are in this Set.

  #### Parameters

  - ##### coll: Iterable<T>

    the collection of items to check for.

  #### Returns boolean

### containsAny

- containsAny(coll: Iterable<T>): boolean

- Returns true if any of the values of a given collection are in this Set.

  #### Parameters

  - ##### coll: Iterable<T>

    the collection of items to check for.

  #### Returns boolean

### `Virtual` copy

- copy(): Set<T>

- Makes a shallow copy of this Set. The values are not copied, so if they are objects they may continue to be shared with the original Set.

  #### Returns Set<T>

  The new Set with the same elements.

### delete

- delete(val: T): boolean

- Removes a value (if found) from the Set.

  Be careful not to call this method while iterating over the collection.

  #### Parameters

  - ##### val: T

    The value to remove.

  #### Returns boolean

  true if the value was found and removed, false otherwise.

### `Virtual` each

- each(func: ((a: T) => void)): Set<T>

- Call the given function on each item in the collection.

  #### Parameters

  - ##### func: ((a: T) => void)

    This function must not modify the collection.

    - - (a: T): void

      - #### Parameters

        - ##### a: T

        #### Returns void

  #### Returns Set<T>

  This Set itself.

### first

- first(): T

- Returns the first item in the collection, or null if there is none.

  #### Returns T

  This returns null if there are no items in the collection.

### forEach

- forEach(callbackFunc: ((value1: T, value2: T, map: Set<T>) => void), thisArg: any): void

- Call a provided function once per each key/value pair in this Set.

  #### Parameters

  - ##### callbackFunc: ((value1: T, value2: T, map: Set<T>) => void)

    a function to call for each value in the Set

    - - (value1: T, value2: T, map: Set<T>): void

      - #### Parameters

        - ##### value1: T

        - ##### value2: T

        - ##### map: Set<T>

        #### Returns void

  - ##### thisArg: any

    a value to use as this when executing callbackFunc

  #### Returns void

  `since`

  3.0

### has

- has(val: T): boolean

- Returns whether the given value is in this Set.

  #### Parameters

  - ##### val: T

    The value to check.

  #### Returns boolean

  Whether or not the value is contained within the Set.

### remove

- remove(val: T): boolean

- Removes a value (if found) from the Set.

  Be careful not to call this method while iterating over the collection.

  #### Parameters

  - ##### val: T

    The value to insert.

  #### Returns boolean

  true if the value was found and removed, false otherwise.

### removeAll

- removeAll(coll: Iterable<T> | T[]): Set<T>

- Removes all of the values of a collection from this Set.

  Be careful not to call this method while iterating over the collection.

  #### Parameters

  - ##### coll: Iterable<T> | T[]

    the collection of items to remove.

  #### Returns Set<T>

  This modified Set.

### retainAll

- retainAll(coll: Iterable<T>): Set<T>

- Removes from this Set all items that are not in the given collection.

  Be careful not to call this method while iterating over the collection.

  #### Parameters

  - ##### coll: Iterable<T>

    the collection of items that should be kept in this Set.

  #### Returns Set<T>

  This modified Set.

### toArray

- toArray(): T[]

- Produces a JavaScript Array from the contents of this Set.

  #### Returns T[]

  A copy of the Set in Array form.

### toList

- toList(): List<T>

- Converts the Set to a List. Because there is no ordering within a Set, the values in the List may be in any order.

  #### Returns List<T>

  A copy of the contents of this Set in List form.

### values

- values(): IterableIterator<T>

- Return a JavaScript iterator that produces the values in this Set. This does not produce the old GoJS style Iterator -- use the iterator property if you want that.

  #### Returns IterableIterator<T>

  `since`

  3.0

Copyright © 1998-2024 by Northwoods Software Corporation.