0

I have a question on Java memory use. It’s for my edification and anyone else who searches and finds this later! For the purpose of the question, please assume, this is a single method and nothing goes out of scope... during my question ;-)

I have created 5 new objects with a single property called ‘name’ of type String. I create an ArrayList and add the 5 objects to the ArrayList. I then create a HashMap and iterate through the previously created ArrayList, adding the objects to the HashMap.

Q1. When I add the objects from the ArrayList, to the HashMap, I assume I am just creating another collection of ‘pointers’, since I’m not using the ‘new’ keyword. Therefore no new memory is consumed, except for the HashMap itself (the objects are not duplicated).

Q2. If I change the value of ‘name’, in an object in the HashMap, would the same change be seen, if I were to iterate over the ArrayList, after making the change.

I appreciate a ‘sanity check’ on my understanding.

3
  • Yes, it will change in ArrayList as well. Commented Jan 1, 2013 at 4:11
  • I think it is unlikely that someone with a similar "need for clarification" would actually find this question by searching. Commented Jan 1, 2013 at 4:19
  • You best write a small program to answer your questions yourself! Commented Jan 3, 2013 at 1:09

3 Answers 3

3

Q1: The HashMap is created and the references to the objects are created. So memory is consumed, but references aren't terribly big, but can make a difference if the number of references is huge.

Q2: Edit: Yes, the name field would change. Better still, write a small program to check it out.

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

6 Comments

The asker was more concerned about object duplication, rather than memory usage per se
@Jan: I agree, but they still should know that there is a small cost to making references.
String is an immutable class. There is no way to change the value without allocating a new object. So while the answer is correct in theory, in practice the entire question (Q1 at least) does not make sense.
@ValekHalfHeart: I'm not sure I agree. The object held by the collection holds a String field. So while it will reference a different String object if the String is "changed" the reference to the object held by the collection is still valid.
@HovercraftFullOfEels it's definitely still valid, but since its not the same reference as in the other collection changes to one reference will not appear in the other.
|
0

A1 : Yes, other than the references and HashMap, nothing new will be created. (Assuming you are not creating a new set of keys for for the HashMap)

A2 : Yes, the change will reflect on the ArrayList.

Comments

0

To answer your questions.

1.) When you add objects to a HashMap the objects are not duplicated. Internally though the map will create new objects to maintain its inner structure. The inner structure of a map consists of HashMap.Entry objects that contain a linked list with all values that map to the same hash code. Thus whenever you add objects to a map one or more internal objects are created.

2.) I assume you stored the objects in the HashMap using their name as key. In this case chaning the name of an object will update the object (no matter whether it's being accessed through the list or the map, it's always the same object) but not the mapping in the map. In the map the object will still be store under its old name!

 Map map = new HashMap();
 Foo f = new Foo();
 f.setName("A");
 map.put(f.getName(),f);
 f.getName(); // => "A"
 map.get("A"); // => f
 f.setName("B");
 f.getName(); // => "B"
 map.get("B"); // => null
 map.get("A"); // => f

Comments

Your Answer

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.