Удалить элементы из коллекции во время итерации
AFAIK, есть два подхода:
Iterate over a copy of the collection Use the iterator of the actual collectionНапример,
List<Foo> fooListCopy = new ArrayList<Foo>(fooList);
for(Foo foo : fooListCopy){
// modify actual fooList
}
а также
Iterator<Foo> itr = fooList.iterator();
while(itr.hasNext()){
// modify actual fooList using itr.remove()
}
Есть ли причины предпочитать один подход другому (например, предпочтение первого подхода по простой причине читабельности)?