Hi all,
I was wondering when I pass a reference object as a key to the put() method from Map interface, such as
Map<Object, Object> m = new HashMap<Object, Object>();
MyObject o = new MyObject();
m.put(o, "Some info");
does the put() method make a new copy of MyObject o in the HashMap, or will the key from HashMap refer to the same object on the heap memory?
I also wrote some tests to try to uncover the answer to this question. I found that whenever I make a change to MyObject o (through o.update(), for example), the object in the HashMap m changes as well.
So I am thinking this is because they are referring to the same object.
But if that is true, then why is it that
o.setName("New name"); // Change to a new name, this is the
// name used in the overriden equals()
// Right now, assuming key reference from HashMap m and o are
// referring to the same object, then the following line should not
// return null
m.get(o);
And m.get(o) does return null.
Any thoughts or answers?
Thanks,
Mack