How can the value with the same key will be overwritten? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can the value with the same key will be overwritten?

Whenever the HashMap class reference is declared it calls the hashcode() and equals() automatically. So, equals() returns false and the new value with the same key will be assigned at different location.

5th Jul 2016, 12:58 PM
Shipra Chordia
Shipra Chordia - avatar
1 Answer
0
I don't think equals() and hashCode() methods are called when the reference to a HashMap is declared. hashCode() is called when we call the put() method and send an object to store in the Map, and it generates a hashcode which is used to specify a slot in which the object will be stored. Similarly, while comparing an object to an object stored in the Map, first hashCode() is called to get the slot, and then equals() is called to actually compare the objects.
12th Jul 2016, 5:09 AM
Chinmay Terse
Chinmay Terse - avatar