Skip to content
Tim Down edited this page Mar 16, 2015 · 1 revision

HashSet

HashSet is similar to HashSet implementations found in Java and C#. It wraps the keys of a jshashtable hash table.

Using HashSet

HashSet depends on jshashtable's HashTable implementation. You will therefore need to include both jshashtable.js and jshashset.js, in that order.

Constructor


HashSet([Function hashingFunction[, Function equalityFunction]])

Creates a new HashSet.

Parameters

  • hashingFunction
    An optional function that provides hash codes for keys placed in the wrapped hash table. It is passed the object to be hashed as its only parameter. If not provided, the hash table checks whether the object has a hashCode() method, and if not, calls toString() on the object.
  • equalityFunction
    An optional function that checks for equality between two keys with the same hash code. Two keys that are considered equal will map to the same value in the hash table. This function is passed the two objects to be compared as its parameters. If not provided, the wrapped hash table checks whether either object being compared has an equals() method, and if not, compares the objects using the === operator.

Methods


void add(mixed value)

Adds the specified object or primitive to the set. value replaces any member of the set equal to it.


void addAll(Array arr)

Adds all members of an array arr to the set in descending order. Each member of arr replaces any member of the set equal to it. Since the order is descending, this means that an earlier member overwrites an equal later member of the array within the set.


Array values()

Returns an array containing all the members of the set in unspecified order.


void remove(mixed value)

Removes the specified value from the set.


Boolean contains(mixed value)

Returns whether the set contains the specified value.


void clear()

Removes all members from the set.


Number size()

Returns the number of members contained in the set.


Boolean isEmpty()

Returns true if the set has no members, false otherwise.


Boolean isSubsetOf(HashSet otherSet)

Returns true if every member this set is also a member of otherSet.


HashSet clone()

Creates and returns a shallow copy of the set. If hashing and equality functions were provided to the set when it was constructed, they are passed into the new set.


HashSet intersection(HashSet otherSet)

Creates and returns a new HashSet containing those elements that are contained in both this set and otherSet.


HashSet union(HashSet otherSet)

Creates and returns a new HashSet containing those elements that are contained in one or both of this set and otherSet.


HashSet complement(HashSet otherSet)

Creates and returns a new HashSet containing those elements that are contained in this set but not otherSet.