1

In my code I have an ArrayList contentChecklist which stores HashMap objects in it. After adding all the HashMap objects in the List, and if I get the last HashMap object from the list and store it in new HashMap object (CheckListMaptemp) and use CheckListMaptemp.put("place", "somePlace"); . It is modifying the original HashMap oject present in the List.

public static void main(String[] args) {    
    List<HashMap> contentChecklist = new ArrayList<>();

    Map<String,String> checklIstMap1= new HashMap<>();
    checklIstMap1.put("name", "name1");
    checklIstMap1.put("uuid", "001");

    contentChecklist.add(checklIstMap1);

    Map<String,String>  checklIstMap2= new HashMap<>();
    checklIstMap2.put("name", "name2");
    checklIstMap2.put("uuid", "002");

    contentChecklist.add(checklIstMap2);

    Map<String,String> CheckListMaptemp= (Map<String, String>) contentChecklist.get(contentChecklist.size()-1);

    CheckListMaptemp.put("place", "somePlace");

    for (Iterator iterator = contentChecklist.iterator(); iterator.hasNext();) {
        Map object = (Map) iterator.next(); 
        String val = (String) object.get("place");
        System.out.println(val);
    }
}

The output is :

null

somePlace

I know this is the correct behaviour , but I am eager to know how contentChecklist contents gets modified when we invoke CheckListMaptemp.put("place", "somePlace"); on CheckListMaptemp , even though both are different objects .

Community
  • 1
  • 1
jaseel_ep
  • 58
  • 8
  • 1
    SideNote: please don't use rawtypes! See https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it/2770692 – Hulk Jan 24 '19 at 12:46
  • I have added Generics , But it doesn't make any difference in output right? – jaseel_ep Jan 24 '19 at 12:51
  • 1
    No, it does not change the behavior here. Just wanted to point this out in case you are not aware of this. – Hulk Jan 24 '19 at 12:52
  • "even though both are different objects". They are *not* different objects. `Map.get` does not make new objects, it gives you (another reference to) the object from the map. – Thilo Jan 24 '19 at 12:52

2 Answers2

2
Map CheckListMaptemp= (Map)contentChecklist.get(contentChecklist.size()-1);

The variable CheckListMaptemp is a reference to the Map object contained in the list at position contentChecklist.size()-1. So, whatever changes you make using that reference will be reflected in that Map object. The reference DOES NOT point to another Map object.

Prashant Pandey
  • 4,332
  • 3
  • 26
  • 44
2

This is how Collection works. Whenever you added any object inside a collection, your collection just point to the same object. It won't creates new object.

In your case, you created two HashMap object checklIstMap1 and checklIstMap2. When you added it in ArrayList i.e. contentChecklist, Your ArrayList just hold the reference of both the map. It won't creates new Map and added it in List.

So when you do Map<String,String> CheckListMaptemp= (Map)contentChecklist.get(contentChecklist.size()-1);. It just returns the reference of the map object, It won't creates new Object and returns.

So if you will do any modification it will reflect in the same object.

Deepak Kumar
  • 1,246
  • 14
  • 38