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.
If its green its biology if its stinkks its chemistry if it has numbers it is Maths and if it doesn't work its TECHNOLOGY
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.
// Initializing
myObj.setName("some_name");
// Add to HashMap object
map.put(myObj, "some_value");
// 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?
Thanks,
Mack
Thanks and Regards,<br />Manish Agarwal<br />SCJP 1.4 (100%)<br />SCWCD 1.4 (98%)<br />SCBCD 1.3 (Preparing)
If its green its biology if its stinkks its chemistry if it has numbers it is Maths and if it doesn't work its TECHNOLOGY
If its green its biology if its stinkks its chemistry if it has numbers it is Maths and if it doesn't work its TECHNOLOGY
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|