简体   繁体   English

将hashmap的arraylist放入另一个

[英]Put arraylist of hashmap into another

I want to put an ArrayList<HashMap<String, Object>> into another ArrayList<HashMap<String, Object>> . 我想将ArrayList<HashMap<String, Object>>放入另一个ArrayList<HashMap<String, Object>> How can I do that? 我怎样才能做到这一点?

I was trying with the assignment operator ( = ) but I found out that it just points to the reference, I want to put the elements into another. 我尝试使用赋值运算符( = ),但是发现它只是指向引用,我想将元素放入另一个。

您可以使用addAll方法,但是请记住,它不会复制HashMaps,而且不会复制其中的Object!

Try this: 尝试这个:

List<HashMap<String, Object>> newList = new ArrayList<HashMap<String,Object>>();
for (HashMap<String, Object> hm: oldList) {
    newList.add(hm);
}

You can create a new ArrayList Object and just call the addAll method and pass the ArrayList you want to copy 您可以创建一个新的ArrayList对象,然后调用addAll方法并传递要复制的ArrayList。

Another way is Collections.copy(desc,src) but this is not feasible since the size of both the List must be equal. 另一种方法是Collections.copy(desc,src)但这是不可行的,因为两个List的大小必须相等。

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

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