Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does HashSet have "Hash" in its name?

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.

like image 763
zengr Avatar asked Nov 03 '10 01:11

zengr


2 Answers

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.

like image 139
Affe Avatar answered Oct 01 '22 16:10

Affe


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).

like image 32
ColinD Avatar answered Oct 01 '22 17:10

ColinD