Skip to main content
  1. About
  2. For Teams
Asked
Viewed 344 times
2

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.
1
  • 6
    Try it and see....
    duffymo
    –  duffymo
    2012-07-02 01:30:33 +00:00
    Commented Jul 2, 2012 at 1:30

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The same applies in all other cases as well: pulling an object out of an array or other types of Collection, passing the object to a method, having multiple variables point to the same object and so on.

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.