concurrentmodificationexception java code example
Example: ConcurrentModificationException fix
//Occurs when trying to remove an element from a Collection
//while iterating over it
//Where removal takes place
//Setup an iterator
Iterator<Thing> iter = things.iterator();
//Iterate over collection
while(iter.hasNext()) {
Thing thing = iter.next();
if (/* Some Condition that initiates removal */) {
//Remove using iterator
iter.remove();
}
}
//Avoids ConcurrentModificationException