# Class goog.structs.Set.`<T>`

code »

All implemented interfaces:
  `goog.structs.Collection.<(T|null)>`

A set that can contain both primitives and objects. Adding and removing elements is O(1). Primitives are treated as identical if they have the same type and convert to the same string. Objects are treated as identical only if they are references to the same object. WARNING: A goog.structs.Set can contain both 1 and (new Number(1)), because they are not the same. WARNING: Adding (new Number(1)) twice will yield two distinct elements, because they are two different objects. WARNING: Any object that is added to a goog.structs.Set will be modified! Because goog.getUid() is used to identify objects, every object in the set will be mutated.

## Constructor

goog.structs.Set ( opt_values )

Parameters

opt_values: `(Array.<T>|Object)=`
  Initial values to start with.

Show:Public Protected Private

## Instance Methods

code »__iterator__ ( opt_keys ) ⇒ `!goog.iter.Iterator`

Returns an iterator that iterates over the elements in this set.

Parameters

opt_keys: `boolean=`
  This argument is ignored.

Returns

An iterator over the elements in this set.

code »add ( element )

Add a primitive or an object to the set.

Parameters

element: `T`
  The primitive or object to add.

code »addAll ( col )

Adds all the values in the given collection to this set.

Parameters

col: `(Array.<T>|goog.structs.Collection.<T>|Object)`
  A collection containing the elements to add.

code »clear ( )

Removes all elements from this set.

code »clone ( ) ⇒ `!goog.structs.Set.<T>`

Creates a shallow clone of this set.

Returns

A new set containing all the same elements as this set.

code »contains ( element ) ⇒ `boolean`

Tests whether this set contains the given element.

Parameters

element: `T`
  The primitive or object to test for.

Returns

True if this set contains the given element.

code »containsAll ( col ) ⇒ `boolean`

Tests whether this set contains all the values in a given collection. Repeated elements in the collection are ignored, e.g. (new goog.structs.Set([1, 2])).containsAll([1, 1]) is True.

Parameters

col: `(goog.structs.Collection.<T>|Object)`
  A collection-like object.

Returns

True if the set contains all elements.

code »difference ( col ) ⇒ `!goog.structs.Set`

Finds all values that are present in this set and not in the given collection.

Parameters

col: `(Array.<T>|goog.structs.Collection.<T>|Object)`
  A collection.

Returns

A new set containing all the values (primitives or objects) present in this set but not in the given collection.

code »equals ( col ) ⇒ `boolean`

Tests whether the given collection consists of the same elements as this set, regardless of order, without repetition. Primitives are treated as equal if they have the same type and convert to the same string; objects are treated as equal if they are references to the same object. This operation is O(n).

Parameters

col: `(goog.structs.Collection.<T>|Object)`
  A collection.

Returns

True if the given collection consists of the same elements as this set, regardless of order, without repetition.

code »getCount ( ) ⇒ `number`

Returns

The number of elements in the set.

code »getValues ( ) ⇒ `!Array.<T>`

Returns an array containing all the elements in this set.

Returns

An array containing all the elements in this set.

code »`<S>` intersection ( col ) ⇒ `!goog.structs.Set`

Finds all values that are present in both this set and the given collection.

Parameters

col: `(Array.<S>|Object)`
  A collection.

Returns

A new set containing all the values (primitives or objects) present in both this set and the given collection.

code »isEmpty ( ) ⇒ `boolean`

Tests whether this set is empty.

Returns

True if there are no elements in this set.

code »isSubsetOf ( col ) ⇒ `boolean`

Tests whether the given collection contains all the elements in this set. Primitives are treated as equal if they have the same type and convert to the same string; objects are treated as equal if they are references to the same object. This operation is O(n).

Parameters

col: `(goog.structs.Collection.<T>|Object)`
  A collection.

Returns

True if this set is a subset of the given collection.

code »remove ( element ) ⇒ `boolean`

Removes the given element from this set.

Parameters

element: `T`
  The primitive or object to remove.

Returns

Whether the element was found and removed.

code »removeAll ( col )

Removes all values in the given collection from this set.

Parameters

col: `(Array.<T>|goog.structs.Collection.<T>|Object)`
  A collection containing the elements to remove.

## Instance Properties

code »goog.structs.Set.prototype.map_ : `goog.structs.Map`

Class for Hash Map datastructure.

## Static Functions

code »goog.structs.Set.getKey_ ( val ) ⇒ `string`

Obtains a unique key for an element of the set. Primitives will yield the same key if they have the same type and convert to the same string. Object references will yield the same key only if they refer to the same object.

Parameters

val: `*`
  Object or primitive value to get a key for.

Returns

A unique key for this value/object.