This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Peter when you add a key-value to a Map, the key-value is placed in a bucket based on the hash code. If after that you modify the key in such a way that it changes the hash code of the key, then your map gets sort of corrupt. The key-value is still in the Map but you will not be able to find it as its still in the old bucket in which it went when you added it...
d1.name = "magnolia"; //Are we modifying the actual key in the map??? the key referenced by d1???
In this case you are modifying object d1 and when you invoke m.get (d1), Hashmap invoke hash method on d1 object and no bucket are found in HashMap. Then return null.
d1 not referts to the actual key in the map. When you pass d1 to get method, hashmap invoke your hascode() method at d1 and if bucket is found at hash table, then invoke your d1 equals method to find a match object (based on equals mehtod not the reference).
Joined: Mar 03, 2011
Thank you for your reply Ankit and Sergio... I will let you know if I have more questions.