Is there a more efficient/quicker/more sensible to copy part of an ArrayList than the way I’m doing it?
public ArrayList<FooObject> getListOfFlagged() {
for(FooObject fooObject: foos) {
//for each item in the original array, where the item isFlagged...
if(fooObject.isFlagged) {
someOtherArray.add(fooObject);
}
}
return someOtherArray;
}
You can use
Collections2.filter()method fromguava. It’ll look more functionally:The result is backed by your original
fooscollection, so if you need a copy, then you have to make a defensive copy withnew ArrayList<FooObject>(filteredCollection).