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 approach over the other (e.g. preferring the first approach for the simple reason of readability)?
Best Answer
Let me give a few examples with some alternatives to avoid a
ConcurrentModificationException
.Suppose we have the following collection of books
Collect and Remove
The first technique consists in collecting all the objects that we want to delete (e.g. using an enhanced for loop) and after we finish iterating, we remove all found objects.
This is supposing that the operation you want to do is "delete".
If you want to "add" this approach would also work, but I would assume you would iterate over a different collection to determine what elements you want to add to a second collection and then issue an
addAll
method at the end.Using ListIterator
If you are working with lists, another technique consists in using a
ListIterator
which has support for removal and addition of items during the iteration itself.Again, I used the "remove" method in the example above which is what your question seemed to imply, but you may also use its
add
method to add new elements during iteration.Using JDK >= 8
For those working with Java 8 or superior versions, there are a couple of other techniques you could use to take advantage of it.
You could use the new
removeIf
method in theCollection
base class:Or use the new stream API:
In this last case, to filter elements out of a collection, you reassign the original reference to the filtered collection (i.e.
books = filtered
) or used the filtered collection toremoveAll
the found elements from the original collection (i.e.books.removeAll(filtered)
).Use Sublist or Subset
There are other alternatives as well. If the list is sorted, and you want to remove consecutive elements you can create a sublist and then clear it:
Since the sublist is backed by the original list this would be an efficient way of removing this subcollection of elements.
Something similar could be achieved with sorted sets using
NavigableSet.subSet
method, or any of the slicing methods offered there.Considerations:
What method you use might depend on what you are intending to do
removeAl
technique works with any Collection (Collection, List, Set, etc).ListIterator
technique obviously only works with lists, provided that their givenListIterator
implementation offers support for add and remove operations.Iterator
approach would work with any type of collection, but it only supports remove operations.ListIterator
/Iterator
approach the obvious advantage is not having to copy anything since we remove as we iterate. So, this is very efficient.removeAll
approach the disadvantage is that we have to iterate twice. First we iterate in the foor-loop looking for an object that matches our removal criteria, and once we have found it, we ask to remove it from the original collection, which would imply a second iteration work to look for this item in order to remove it.Iterator
interface is marked as "optional" in Javadocs, which means that there could beIterator
implementations that throwUnsupportedOperationException
if we invoke the remove method. As such, I'd say this approach is less safe than others if we cannot guarantee the iterator support for removal of elements.