Use iterator to safely remove from a collection while iterating over it:
Example:
List<String> names = ....
Iterator<String> i = names.iterator();
while (i.hasNext()) {
String s = i.next(); // need to be called before you can call i.remove()
// your work
i.remove();
}
This is stated in Java Documentation:
The iterator returned by this class's iterator and listIterator methods is fail-fast, meaning, if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
Possibly what is confusing to many beginners is the fact that iterating over a list using the for/foreach constructs inevitably builds an iterator which is necessarily inaccessible. You can read about it here.