The goog.structs.StringSet Class

goog.structs.StringSet(opt_elements)

Creates a set of strings.

opt_elements {!Array=}
Elements to add to the set. The non-string items will be converted to strings, so 15 and '15' will mean the same.

.__iterator__(opt_keys)

Returns an iterator that iterates over the elements in the set. NOTE: creating the iterator copies the whole set so use {@link #forEach} when possible.

opt_keys {boolean=}
Ignored for sets.
returns {!goog.iter.Iterator}
An iterator over the elements in the set.

.add(element)

Adds a single element to the set.

element {*}
The element to add. It will be converted to string.

.addArray(arr)

Adds a the elements of an array to this set.

arr {!Array}
The array to add the elements of.

.addSet(stringSet)

Adds a the elements of a set to this set.

stringSet {!goog.structs.StringSet}
The set to add the elements of.

.clear()

Removes all elements of the set.

.clone()

returns {!goog.structs.StringSet}
Clone of the set.

.contains(element)

Tells if the set contains the given element.

element {*}
The element to check.
returns {boolean}
Whether it is in the set.

.containsArray(arr)

Tells if the set contains all elements of the array.

arr {!Array}
The elements to check.
returns {boolean}
Whether they are in the set.

.equals(stringSet)

Tells if this set has the same elements as the given set.

stringSet {!goog.structs.StringSet}
The other set.
returns {boolean}
Whether they have the same elements.

.forEach(f, opt_obj)

Calls a function for each element in the set.

f {function(string, undefined, !goog.structs.StringSet)}
The function to call for every element. It takes the element, undefined (because sets have no notion of keys), and the set.
opt_obj {Object=}
The object to be used as the value of 'this' within {@code f}.

.getCount()

Counts the number of elements in the set in linear time. NOTE: getCount is always called at most once per set instance in google3. If this usage pattern won't change, the linear getCount implementation is better, because

  • populating a set and getting the number of elements in it takes the same amount of time as keeping a count_ member up to date and getting its value;
  • if getCount is not called, adding and removing elements have no overhead.
    returns {number}
    The number of elements in the set.

    .getDifference(stringSet)

    Calculates the difference of two sets.

    stringSet {!goog.structs.StringSet}
    The set to subtract from this set.
    returns {!goog.structs.StringSet}
    {@code this} minus {@code stringSet}.

    .getIntersection(stringSet)

    Calculates the intersection of this set with another set.

    stringSet {!goog.structs.StringSet}
    The set to take the intersection with.
    returns {!goog.structs.StringSet}
    A new set with the common elements.

    .getSymmetricDifference(stringSet)

    Calculates the symmetric difference of two sets.

    stringSet {!goog.structs.StringSet}
    The other set.
    returns {!goog.structs.StringSet}
    A new set with the elements in exactly one of {@code this} and {@code stringSet}.

    .getUnion(stringSet)

    Calculates the union of this set and another set.

    stringSet {!goog.structs.StringSet}
    The set to take the union with.
    returns {!goog.structs.StringSet}
    A new set with the union of elements.

    .getValues()

    returns {!Array.<string>}
    The elements of the set.

    .isDisjoint(stringSet)

    Tells if this set and the given set are disjoint.

    stringSet {!goog.structs.StringSet}
    The other set.
    returns {boolean}
    True iff they don't have common elements.

    .isEmpty()

    returns {boolean}
    Whether the set is empty.

    .isSubsetOf(stringSet)

    Tells if this set is the subset of the given set.

    stringSet {!goog.structs.StringSet}
    The other set.
    returns {boolean}
    Whether this set if the subset of that.

    .isSupersetOf(stringSet)

    Tells if this set is the superset of the given set.

    stringSet {!goog.structs.StringSet}
    The other set.
    returns {boolean}
    Whether this set if the superset of that.

    .remove(element)

    Removes a single element from the set.

    element {*}
    The element to remove.
    returns {boolean}
    Whether the element was in the set.

    .removeArray(arr)

    Removes all elements of the given array from this set.

    arr {!Array}
    The elements to remove.

    .removeSet(stringSet)

    Removes all elements of the given set from this set.

    stringSet {!goog.structs.StringSet}
    The set of elements to remove.