2

I wish to add list to map, but unable to get the desired output as a map. Any way to get the below desired output?

    List l = new ArrayList();

    Map<String, List<Object>> mp = new HashMap();

    l.add("A");

    mp.put("1", l);

    l.clear();
    l.add("B");

    mp.put("2", l);

    System.out.println(mp);

Expected :{1=[A], 2=[B]}

Actual :{1=[B], 2=[B]}

Barca Fan
  • 47
  • 4

3 Answers3

3

Even though you clear the values in the list, the Map is still referencing the original list, so any changes you make to that list will affect the original location as well.

Take a look at this example modified from your code:

Map<String, List<Object>> mp = new HashMap<>();

for (int k = 65; k <= 91; k++) {
    List<Object> l = new ArrayList<>();
    l.add((char)k);
    mp.put(Integer.toString(k), l);
}

System.out.println(mp);

This will print a Map pairing for each letter A to Z with the respective ASCII values for the letter.

{66=[B], 88=[X], 67=[C], 89=[Y], 68=[D]...etc...

However what happens if you change the code to clear the list like you have in your code right before the print and only declare a new List a single time outside of the loop:

Map<String, List<Object>> mp = new HashMap<>();

List<Object> l = new ArrayList<>();
for (int k = 65; k <= 91; k++) {
    l.add((char)k);
    mp.put(Integer.toString(k), l);
}

l.clear();
System.out.println(mp);

This will now print this:

{66=[], 88=[], 67=[], 89=[], 68=[], 69=[], 90=[] ...etc...

With every List value in the Map blank because they were all referencing the same list that was cleared at the end.

Now it may seem obvious that they would all be cleared because inside of the loop we did not clear the List each iteration and just added every value into the List all together, so clearly the Map is referencing the same List! However, using clear has nothing to do with referencing the same list or not, it just makes it seem like you did not just add all of the values into the same list, but you did.

Nexevis
  • 4,647
  • 3
  • 13
  • 22
  • Thanks a lot, it helped – Barca Fan Aug 01 '19 at 15:46
  • @Barca Fan Sure thing, also I recommend looking into the difference between `List` and `List` because they are not the same thing. – Nexevis Aug 01 '19 at 15:48
  • `char` is a UTF-16 code unit, one or two of which encode a Unicode codepoint; Nothing to do with ASCII. – Tom Blodget Aug 03 '19 at 16:15
  • @Tom Budget I was not speaking really about what was happening behind the scenes there and more that the point of the code was to print the ASCII table values as its functionality, which are the same for UTF16 and ASCII. I don't feel like specifics on char are relevant to the question. Like say the method it was in was `displayAscii`, you might use UTF16 to do it, but that doesnt mean 65 is not A in ASCII. – Nexevis Aug 03 '19 at 18:57
  • That's a lot of explaining about a character encoding that Java doesn't use for text datatypes and the question doesn't ask about. – Tom Blodget Aug 04 '19 at 02:47
  • @Tom Blodget What are you even talking about? This is just an example to explain why the `list` changes when you do not create a `new` one. I did not explain anything about encoding or text data types at all. Clearly it helped him understand why what was happening with `list`. Its an _example_. – Nexevis Aug 04 '19 at 03:57
0
    List l = new ArrayList();

    Map<String, List<Object>> mp = new HashMap();

    l.add("A");
    mp.put("1", l);

    l = new ArrayList();        
    l.add("B");
    mp.put("2", l);

    System.out.println(mp);
Anant Goswami
  • 318
  • 3
  • 14
0

List was pass-by-reference so you need to create two different List

List l1 = new ArrayList();
l1.add("A");

List l2 = new ArrayList();
l2.add("B");

Map<String, List<Object>> mp = new HashMap();
mp.put("1", l1);
mp.put("2", l2);

System.out.println(mp);