KuLdip PaTel
KuLdip PaTel

Reputation: 1069

My Hashmap object value list object return only last object i was put in ArrayList<Map<String,Object>>>();

this is my code for detail description. i get size of Hashmapis ok. but when i try to get ArrayList<Map<String, Object>>> using key, all the ArryList have size 1.

private ArrayList<Map<String, Object>> settingInObject;
private ArrayList<Map<String, Object>> parentItemList;
private HashMap<String, ArrayList<Map<String, Object>>> childItemList;

settingInObject = new ArrayList<>();
parentItemList = new ArrayList<>();
childItemList = new HashMap<String, ArrayList<Map<String,Object>>>();


   ArrayList<Map<String, Object>> arrayList = new ArrayList<Map<String, Object>>();
    for (Map<String, Object> objectMap : settingInObject) {
        if (objectMap.get("IsCheked").toString().equals("1")) {
            if (arrayList.size() > 0) {
                childItemList.put(parentItemList.get(parentItemList.size()).get("TitleDesc").toString().trim(), arrayList);
                arrayList.clear();
            }
            parentItemList.add(objectMap);
        } else {
            arrayList.add(objectMap);
        }
    }

is there i do something wrong??

Upvotes: 2

Views: 479

Answers (1)

m.antkowicz
m.antkowicz

Reputation: 13571

You are calling

arrayList.clear();

while under arayList you have still reference to added object - in result you are clearing the list that is already put in the map

If you want to have new empty list in arrayList you need to reinstance it rather

arrayList = new ArrayList<Map<String, Object>>();

Upvotes: 3

Related Questions