Why is Hashset called a "Hash"-set?
I understand we call hashtable or a hashmap since it's a key value store and when we put(), then the key is hashed and distributed evenly using a good hash function.
I assume its called a HashSet because when we add(), the value is hashed and stored to keep it unique. But why the overkill? We don't really care about "equal distribution" of data like we do in a Hash table.
We do care about equal distribution because we want constant time performance on our basic Collection
operations. In order to respect the basic rules of a SET
, no two objects are equal, we want to find a potentially equal match quickly. HashSet
is one fairly good way of doing it. Compare to a theoretical ArraySet
where adding a new element is a linear time operation to iterate and check every single existing entry for equality.
A HashSet
is called a HashSet
because hashing is indeed important to its functionality. Operations like contains(Object)
(arguably the most important method in a Set
) and remove(Object)
are able to work in constant time by making use of the hash code of the object (by way of HashMap
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With