Set CLASS

A hash set to store data based on the calculated hash value, allowing fast element-targeting operation at O(1) cost.

An object stored in set will provide its hash through Object.hashCode() method. A primitive value will convert is numeric value into an integer to be used as hash. For example, byte value simply promotes the value to integer, and a float value get floored to an integer.

The hashcode is only a means to locate the slot in the map, but doesn't have to be unique. The uniqueness is determined by Object.equals() method. When putting a value into a set, if the slot suggested by the hashcode is already occupied, Object.equals() is called between the new object and existing one. If the method returns true, the new object is not added; otherwise, the object will be added anyway. If there is more than one object using that slot, each of them will be compared against before the new object can be cleared of its uniqueness and added. The multiple objects stored on the same map slot are internally organized by a linked list. In best practice, try to make the hashcode as unique as possible to improve the performance, and always pay attention to equals() to ensure its proper functioning in determining the logical equality between two objects.

Set is iterable:

   for (var value : set) {
     Console.println(value);
   }


The methods of this class are not thread safe.

Parent Class

Parent Interfaces

All Members


TypeNameSignature
constructorSetpublic Set()
methodaddpublic void add(var)
methodgetAllpublic var[] getAll()
methodgetIteratorpublic IIterator getIterator()
methodhaspublic bool has(var)
methodremovepublic bool remove(var)
methodsizepublic int size()

Constructors


public Set()

Create a new set instance.


Methods


public void add(var value)

Add a value into the set. The value's hashCode() is called to calculate the storage location, and its equals() method is used to determine the uniqueness.

Parameters

  • value The value to the map.

Throws


public var[] getAll()

Get all the values stored in this set.

Returns

  • All the values stored in this set.

public IIterator getIterator()

Get an iterator of this set.

Returns

  • An iterator which yields values stored in this set.

public bool has(var value)

Check if the specified value exists in the set.

Parameters

  • value The value to query about.

Returns

  • true if the value exists; false otherwise.

public bool remove(var value)

Remove the specified value from the set.

Parameters

  • value The value to find in the set.

Returns

  • True if the value existed and removed as the result of this operation; false if the value didn't exist and this operation was a no-op.

public int size()

The size of set.

Returns

  • Always non-negative. 0 if empty.