Sun
Sun

Reputation: 2688

Understanding Java References in Java collections

I am having trouble understanding some simple code relating with Lists and maps. Take the code below as an example:

public class test {

private Map<Integer, List<String>> myMap ;

public test(){
    myMap = new HashMap<Integer, List<String>>();
}

public void addToMap(String ss){

    List<String> myTemp = myMap.get(ss);
    Random  r = new Random();
    if(myTemp == null){

        myTemp = new ArrayList<String>();
        myMap.put(r.nextInt(100), myTemp);
    }

    myTemp.add(ss);

}

public Map<Integer, List<String>> getMap1(){
    return myMap;
}


public  static void main(String args[]){

    test myTest = new test();
    myTest.addToMap("abdc");
    myTest.addToMap("eeer");
    System.out.println(myTest.getMap1());

}

}

How exactly does the addToMap() add a new element to the mylist Map. More specifically, how does myTemp.add(ss) add a new element to myMap when myTemp is a local variable and gets deleted once its done executing. Moreover, removing myTemp.add(ss) from the addToMap() method prints out an empty HashMap in the main method, why is this? How does the put method insert the element into the map when it is executed before the add method? Thanks.

Edit: I edited the code to make a little more sense.

Upvotes: 0

Views: 147

Answers (4)

Jeff Watkins
Jeff Watkins

Reputation: 6359

From a best practice point of view, calling a HashMap "mylist" is problematic in a number of ways. It isn't orthogonal and "mylist" doesn't tell you a lot about its usage.

In answer to your question, it's a simple case of, if the item exists in the map, it is retrieved and the string added to it, if not the list is created and added to the map. As long as the map is in scope, the references to any lists added to it (even if they were created in the scope of a function) remain in scope themselves.

However, this isn't a helpful example at all as it mixes its naming and provides no real world use-case.

Upvotes: 1

Kamran Amini
Kamran Amini

Reputation: 1062

myTemp is a reference to the list. The list is allocated in heap memory and myTemp is actually is a pointer stored on the stack. Whenever you exit the function it gets deleted but the memory allocated in heap memory won't get deleted. Memories allocated in heap memory release when there is no reference to them by Garbage Collector.

You are right. When you leave the addToMap() method, the myTemp will be released(Actually it will be poped from the stack) but the list which is allocated in heap memory space will remain. Everything which is created using new keyword, will be placed inside heap memory space.

Read this link Java Reference Objects

Upvotes: 1

Carl Manaster
Carl Manaster

Reputation: 40346

myTemp isn't the thing - the list - it is instead a reference to the thing. But it isn't that specific reference which you are putting into the map; it is the thing to which it refers. The map has its own reference to the thing, so deleting the myTemp reference doesn't affect the map.

Upvotes: 2

SJuan76
SJuan76

Reputation: 24885

Unlike C++, in Java all variables are references to the real objects. So, when you do

myTemp = new Object();

you create an object in the heap whose reference is copied to the stack variable myTemp. If the reference is added to the list (or other variable), a copy of the reference is added there.

Once myTemp is destroyed, the object continues alive. The exception is when all the references to the object are gone; then the object can not be reached by the code and Garbage Collection can (but it is not forced to) delete it from memory.

Upvotes: 2

Related Questions