【发布时间】:2016-03-30 18:17:21
【问题描述】:
我正在尝试编写一个方法,当传递一个数组列表时,它将返回一个新的数组列表,其中仅包含与指定条件匹配的成员(不使用迭代)。集合类有一个类似的 removeIf,但如果存在任何此类功能,我正在寻找“copyIf”。例如:
public class Foo {
public int value;
public Foo(int val){
value = val;
}
}
public class Bar {
private ArrayList<Foo> refList;
public ArrayList<Foo> overTwentyList;
public Bar(){
refList = new ArrayList<Foo>();
refList.add(new Foo(12));
refList.add(new Foo(23));
refList.add(new Foo(6));
refList.add(new Foo(44));
refList.add(new Foo(2));
refList.add(new Foo(19));
refList.add(new Foo(99));
refList.add(new Foo(74));
overTwentyList = overTwenty(refList);
// now overTwentyList would contain only Foo members
// with values 23, 44, 99, 74 and refList would retain
// all values
}
public ArrayList<Foo> overTwenty(ArrayList<Foo> bankList){
// bankList filter code here
// normally i would use iteration but i think
// calling this method frequently would eat a lot
// of system resources if it iterates every time
return filteredList;
}
}
【问题讨论】:
-
而不是迭代?在后台几乎总是在迭代。
标签: java arraylist collections filter