HashSet
uses a hasing function to locate elements. Evaluating in it is O(1).
For example, if we have a hash table for storing names of employees, we would use the function as the sum of ASCII chars in it:
f(name) = sum(ascii chars)
and
f('ahmed') = 'a' + 'h' + 'm' + 'e' + 'd' = 97 + 104 + 109 + 101 + 100 = 511.
so, 'ahmed' will be stored at location 511.
However, the previous hash function is very bad and will cause a lot of conflicts when avaluated with names that produce the same sum. Hence, finding a good hash function is very critical goal in hash table implementation.
Refer to the Hash Table data structure for more information.