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
Type | Name | Signature |
---|---|---|
constructor | Set | public Set() |
method | add | public void add(var) |
method | getAll | public var[] getAll() |
method | getIterator | public IIterator getIterator() |
method | has | public bool has(var) |
method | remove | public bool remove(var) |
method | size | public int size() |
public Set()
Create a new set instance.
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
Throws
public var[] getAll()
Get all the values stored in this set.
Returns
public IIterator getIterator()
Get an iterator of this set.
Returns
public bool has(var value)
Check if the specified value exists in the set.
Parameters
Returns
public bool remove(var value)
Remove the specified value from the set.
Parameters
Returns
public int size()
The size of set.
Returns