NucS
NucS

Reputation: 629

about HashMap.get(key) function

If I pull out an object out of my HashMap, and then modify it. Will it modify inside the HashMap also? Or, do I need to set it back again to the HashMap after modifying it?

For example:

HashMap<Integer,TwoPoints> loc = new HashMap<Integer,TwoPoints>();
...
...
TwoPoints tp = loc.get(Id); //pulls out the object
tp.setPoint(group, new Point(x,y); //a method to set something inside the object I pulled.

Upvotes: 2

Views: 341

Answers (1)

Chris Dargis
Chris Dargis

Reputation: 6043

You are getting a reference to the actual object, so any change would be reflected in it. You are not creating a copy.

Update:

Since I used the term "reference", I thought it is important to point out the definition. Under Java's hood, things are not quite as simple as this. But a reference (what you are doing) is getting the value of the pointer to the object.

Upvotes: 5

Related Questions