Can you please explain me the output and how hascode and equals has worked here, as if I comment the overridden hascode and equals methods still the output is the same so confused in hashcode equals contract.
Do I read this correctly?" It looks like you only put one entry into the Map, but pulled and printed it twice.
Some people, when well-known sources tell them that fire will burn them, don't put their hands in the fire.
Some people, being skeptical, will put their hands in the fire, get burned, and learn not to put their hands in the fire.
And some people, believing that they know better than well-known sources, will claim it's a lie, put their hands in the fire, and continue to scream it's a lie even as their hands burn down to charred stumps.
Carey Brown wrote:What makes you think these two objects have the same key? Note that 'id' is not the key the way it has been written.
Thanks for your reply. I am not sure but as I have putted with String "emp1" as a key in map.put("emp1", emp1);
And after getting the object from the key "emp1" and I have just changed the Name property of object Employee.
I guess it has to replace the existing objects name from "Anand" To "KKC" but seems like it has been added into the map.
And if key is not the same then how it has inserted second value into the map as I have changed the name of existing object of the map.
Thanks a lot, please correct if I am wrong anywhere
Tim Holloway wrote:Do I read this correctly?" It looks like you only put one entry into the Map, but pulled and printed it twice.
Yes you have read it correctly, when I have tried this I am also bit confused with HashMap working.
I don't know how it has been inserted the value into the hashmap when I have called setName property.
You are correct in that you have changed the Name of the existing object in the map. You have not added a second object in the map; when you print out the Map, there is still only one object in there. What happens if you put in another object with the key "emp2"? Try that and see what happens when you print out the map; you should see one object associated with emp1 and one object associated with emp2.
Perhaps you are confused by the fact that when you get the object from the map, it does not create a copy of the object. You have a reference to the object locally (through the emp1 variable) and one in the map. Since both references refer to the same object, any change to the object is visible by all the references, so when you print out the map it is printing out the changed value.
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
And as you said "Since both references refer to the same object, any change to the object is visible by all the references, so when you print out the map it is printing out the changed value."
Then why it has stored into the same when I have only get the object and setname to "KKC". Sorry but still I am not getting could you please explain more.