Does HashMap
use the hashCode
and equals
methods of the key or value to store its entries, i.e., which is the class whose hashCode
and equals
methods we need to override? Should it be the key's class (K
), or the value's class (V
)?
The value is essentially irrelevant to the Map itself: it's only the key which is considered by the HashMap
when it is deciding where to put the key/value pair within its internal data structure.
However, you might want to override it for the value too, e.g. if you need to do map.values().contains(...)
.
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