我有一个 ArrayList 对象,其中填充了许多 ArrayList 对象。一些条目是 null,我只想将 not null ArrayLists 的值放入单个 ArrayList。我说的是类似下面的内容。
{null,[p1,p2,p3],[p2,p5,p6],[p4]}
最终需要的输出是这样的。
[p1,p2,p3,p2,p5,p6,p4]
ArrayList 对象的ArrayList 对象 是从 HashMap 对象
创建的。我的代码段如下。但它有问题。
Collection<ArrayList<MyProduct>> tmp = new ArrayList<ArrayList<MyProduct>>();
tmp= orderAdap.values();
ArrayList<MyProduct> flattenList = new ArrayList<MyProduct>();
for(ArrayList<MyProduct> list : tmp){
for(MyProduct i : list)
if(i!=null){
lstStyle.add(i);
}
}
所以有人可以帮助我。谢谢!!!!!!
最佳答案
这样做
tmp.remove(Collections.singleton(null));
for(ArrayList<MyProduct> list : tmp){
lstStyle.addAll(list);
}
关于java - 从其他一些 Arraylist 的元素填充 Arraylist。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20234695/