#HashCode #HashMap #EqualsTo #Map
In this challenge, you will see how a key from a map behaves, how an int type can be widened to an Object.
Try to solve the challenge by yourself first.
Here, we need to understand the how the map uses equals() and hashcode() method while putting the object to the table(*Map).
When map1 will try to put key: 1 and value: ”A Java Challenge”, the
1 will be widened to Integer Object(
new Integer(1) )and will be placed to the map.
If you see, map1, map2 and map3 have same key and value that means all three map have same hashcode and they are equilavlent to each other. The following statement will prove that.
When we will try to put all three maps to mainMap then the method will check equals() and hashcode() for each entry(map1, map2, map3) and try to put. Values will get replaced and the recent one will be in the mainMap.
Try executing above code. All three statement will print
Crazy map 3 .
That’s all about the challenge.
If you liked it, please share.