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