简体   繁体   中英

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??

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>>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM