Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Hashing work for HashSet?

I have just read and understood how hashing works with HashMap: how it uses a hashtable, makes hashcodes with a hash function etc.

My question is: If the same principles apply to HashSet? Well obviously it cannot compute a hashcode from HashSet's key, because it doesn't have one. But does it compute the hashcode from the value of the HashSet then?

like image 617
Langkiller Avatar asked Oct 11 '25 19:10

Langkiller


1 Answers

Actually, a HashSet is backed by HashMap that maps all its keys to a single constant object. The key-set of this map acts as the set. See for yourself by looking at the HashSet source. So yes, the mechanisms by which the two operate are very similar.

like image 122
arshajii Avatar answered Oct 14 '25 09:10

arshajii



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!