简体   繁体   English

我的Hashmap对象值列表对象仅返回我放在ArrayList中的最后一个对象 <Map<String,Object> &gt;&gt;();

[英]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. 我得到Hashmapis的大小确定。 but when i try to get ArrayList<Map<String, Object>>> using key, all the ArryList have size 1. 但是当我尝试使用键获取ArrayList<Map<String, Object>>> ,所有ArryList的大小均为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 而在arayList下,您仍然引用添加的对象-结果您正在清除已放入地图中的列表

If you want to have new empty list in arrayList you need to reinstance it rather 如果要在arrayList有新的空列表, arrayList需要重新实例化

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何从地图中获取价值 <Object,List<Object> &gt; obj = new HashMap <Object,List<Object> &gt;(); - How can i retrieve value from Map<Object,List<Object>> obj=new HashMap<Object,List<Object>>(); 对Hashmap的ArrayList进行排序 <String,Object> 按整数值 - Sorting an ArrayList of Hashmap<String,Object> by int value 如何使用 stream 过滤器获取 hashmap 列表? ArrayList <hashmap<string, object> &gt; </hashmap<string,> - How use stream filter for list of hashmap? ArrayList<HashMap<String, Object>> Java中的ArrayList仅打印列表中的最后一个对象 - ArrayList in Java only prints the last object in the List HashMap(key: String, value: ArrayList) 返回一个 Object 而不是 ArrayList? - HashMap(key: String, value: ArrayList) returns an Object instead of ArrayList? HashMap中 <String, Object> - 如何返回只有参数的对象? - HashMap<String, Object> - How to return an Object having only its argument? 如何正确结合Hashmap <String, ArrayList<Object> &gt;和HashMap <String, Object> - How to union correctly Hashmap<String, ArrayList<Object>> and HashMap<String, Object> 按键值排序列表&lt;HashMap &lt;String,Object &gt;&gt; - Sort List < HashMap < String, Object >> by value of key 我如何退货清单 <String> 从我发送到put(Object)到地图的方法中(获取示例) - How do I return List<String> from a method where I sent to put(Object) into a map (got example) 仅将最后一个对象添加到ArrayList - Only the last Object is added to the ArrayList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM