The top voted answer of this question states:
When using a hash-based Collection or Map such as HashSet, LinkedHashSet, HashMap, Hashtable, or WeakHashMap, make sure that the hashCode() of the key objects that you put into the collection never changes while the object is in the collection. The bulletproof way to ensure this is to make your keys immutable, which has also other benefits.
Honestly I don't understand what is the role played by hashCode()
with regards to HashSet
or HashMap
collection objects.
How hashCode()
method of key objects is being used in hash-based Collections?
Which could be the case where hashCode()
of objects change in their lifetime?
Can someone elaborate furtherly about this aspect with some example?