As Henry mentioned,
Second, you should not change the value of the key in a map. The map uses the hashcode() and equals() method to store the location of the values. If the values of the keys change, which can change the hashcode(), I am *not* sure what will happen -- but that may explain why you can't retrieve the values anymore if you change a key.
Originally posted by Guangcheng Zhou:
Yes. But what I am asking is why sometimes when I pass an object to the get() method, the get() method returns null. For example,
Map<Object, Object> map = new HashMap<Object, Object>();
MyObject myObj = new MyObject(); // hashCode() and equals() has already being overriden. These two methods both use name attribute of MyObject.
// Add to HashMap object
// Now modify the value of the name attribute
map.name = "new_name";
// Since HashMap has a reference to myObj, then the object it refers to should be updated as well, according to Henry's post
// Then, hashCode() and equals() should work according to the updated object
map.get(myObj); // Should not return null
However, the last line return null. Why would that happen?