Remove elements from collection while iterating
AFAIK, there are two approaches: Iterate over a copy of the collection Use the iterator of the actual collection For instance, List<Foo> fooListCopy = new ArrayList<Foo>(fooList); for(Foo foo : fooListCopy){ // modify actual fooList } and Iterator<Foo> itr = fooList.iterator(); while(itr.hasNext()){ // modify actual fooList using itr.remove() } Are there any reasons to prefer one … Read more