The right way to remove elements from a collection while iterating
is by using ListIterator.remove() method.
E.g.
ListIterator
while(iter.hasNext()) {
itr.remove();
}
Some developers use following code to remove an element which is
incorrect:
Iterator
while(iter.hasNext()) {
itr.remove();
}
By doing so we get ConcurrentModificationException.
An iterator is first created to traverse the list. But at the same time
the list is changed by remove() method.
In Java, it is not allowed for a thread to modify a collection while
another thread is iterating it. ListIterator provides the capability of
removing an object during traversa