0
public class ListMap {
    HashMap<Integer, List> mp = new HashMap();
    List myList = new ArrayList();
    Integer x = 0;
    Integer y = 5;

    void test() {
    for(Integer i = 0; i < 5; i++) {
        x = y;
        myList.add("check-1a" + i);
        myList.add("check-1a" + i + 1);
        y = null;
        System.out.println(x); // output=5
        mp.put(i, myList);
        myList.clear();
    }
}

1) But after clearing the List with myList.clear() the values that was inside the Map also gets cleared.

I mean to say that the map key remains there but it contains an "empty" List

2) However regarding the Objects x & y, after setting y to null how come x doesn't change?

12
  • Don't use raw types like List and ArrayList. Commented Jan 22, 2015 at 18:08
  • You should fix proper indentation in your example. Commented Jan 22, 2015 at 18:10
  • @David Conrad please elaborate what to use instead of that??? Commented Jan 22, 2015 at 18:12
  • You should use parameterized types, such as (in this case) HashMap<Integer, List<String>>, List<String>, and ArrayList<String>. Commented Jan 22, 2015 at 18:16
  • thanx I will do the same... Commented Jan 22, 2015 at 18:17

2 Answers 2

1

When you add an object to a map (or any other collection), you are adding a reference to that object, not a copy. When you then make changes to the object, these changes will also affect the references in the map.

When you want to store a copy of your list, you need to create a new one. This can be done like this:

mp.put(i, new ArrayList(myList));

An alternative (and in my opinion even better) solution would be to reinitialize myList in the beginning of each loop iteration by setting it to a fresh list object:

myList = new ArrayList();
myList.add("check-1a"+i);
myList.add("check-1a"+i+1);
mp.put(i,myList);

Note that the list doesn't get destroyed when you reinitialize the variable myList. You can think of the object living on inside the map*.

*although a more technically accurate description would be "the object lives on in memory and isn't garbage-collected as long as there is still a reference to it stored in the map"

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

Comments

1

You're adding the same list to the Map multiple times and clearing it each time, so no surprise that its empty. Solution: Don't clear the list, create a new one within the for loop. This way, the Map will hold a unique List for each Integer.

public class ListMap {
     HashMap<Integer,List<String>> mp=new HashMap<>();
     // List<String> myList=new ArrayList<String>(); // **** get rid of
    Integer x=0;
    Integer y=5;
    void test(){
        for(Integer i=0;i<5;i++){
            List<String> myList=new ArrayList<String>();  // ****** here 
            x=y;
            myList.add("check-1a"+i);
            myList.add("check-1a"+i+1);
            y=null;
            System.out.println(x);//output=5
            mp.put(i,myList);
            // myList.clear();  // **** get rid of
        }
   }

Also as per Tom, don't use raw types if possible, and so declare your lists as List<String> and ArrayList<String>.

8 Comments

I handled it but I am unable to understand why the values of List that I put inside the map prior to clearing the list ,clears the values inside the map as well
@ShashiRanjan: you handled what?? You're only creating one List and re-adding this single same cleared List multiple times. Don't do this. The key concept being that there is only one ArrayList object in all your original code.
and in similar way I used two objects X & Y and putting NULL inside the Y object then why X contains the previous values of Y ???
@HovercraftFullOfEels Don't do this. ... since you've mentioned that ... how about your usage of raw types (HashMap and List/ArrayList)? :P
@ShashiRanjan x and y in your code are just variables holding object references, not objects. When you write x=y, you're making x point to the same Integer object as y. Then, when you set y to null, you're setting that variable so that it points to no object. x still contains the reference to the object previously referenced by y.
|

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.