When an object(Say a String "John") is added to the HashMap with a key (Say '1') using the put() method,
i.e. myHashMap.put(1,"John")
So, the HashCode is calculated based on the key (i.e. '1').
Now, a memory location is calculated based on the HashCode and that Key (i.e. '1') along with the value ('John') is added to that particular memory location.
Sounds complex!! Let's make it simple.
For the below statement,
The HashCode is calculated based on the key (i.e. '1'). Say the calculated HashCode is 6. So, the Key (i.e. '1') and the value (i.e. 'John') will get added to the 6th location of memory.
Again for the below statement,
The HashCode is calculated based on the key (i.e. '2'). Say the calculated HashCode is 3. So, the Key (i.e. '2') and the value (i.e. 'Paul') will get added to the 3rd location of memory.
So far we were dealing with the keys as Integers.
i.e. myHashMap.put(1,"John");
Now, let us consider a scenario where there is a 'Human' class, which contains the 'name' and 'age' of a person.
And, we have to associate an address with the 'Human' class. i.e. We have to create a HashMap where 'Human' object will be a Key and the name of the city will be the Value.
i.e. hashMap.put(human1,"Mumbai");
Now, let us look at the below example to understand the scenario better.
So, what we have done in the above example is:
1) Created two Human objects
2) Declare a HashMap and add the Human objects to the HashMap.
3) We are creating a new object 'human3' for holding the details of 'Paul'.
4) We have added the object 'human3' to the HashMap.
But 'human3' contains the details of 'Paul' whose age is '42'. And if we see closely 'Paul' is already in the hashSet, since we have already added 'human2' object.So, the duplicate entry is getting inserted in the HashSet. But why?
Let's understand the cause:
Map internally takes the help of equals() method to check for duplicate Keys.
And if you remember equals() compares the objects by reference. i.e. By its memory location.
Now, when we are trying to insert an object( 'human4' in the above case), java takes the help of equals() method to check by the memory location of the object.
In simple words the equals() method does not check what is stored inside the Object. It only checks if two objects belongs to the same memory location.
The Solution is, we have to override hashCode() and equals() method in our class.
Let us redefine the hashCode() and equals() in the Human class.
We have redefined the equals() method where we have mentioned that the objects should be compared based on value and not on memory location.
Also we have overridden the hashCode() method and put a custom logic. So that the hash code is calculated not based on the memory location(Since the default implementation of hashCode() calculates the hash code based on the memory location).