+ 5
What is Concurrent Modification ?
2 Respuestas
+ 3
java.util.ConcurrentModificationException is a very common exception when working with java collection classes. Java Collection classes are fail-fast, which means if the Collection will be changed while some thread is traversing over it using iterator, the iterator.next() will throw ConcurrentModificationException.
0
TechTro answer is perfect.
I just want to add that it can also happen in a single thread.
It basically happens if you modify a list or other iterable while iterating over it using an iterator.
This also happens if you use
for (String s:list){
if (s.equals ("")
list.remove (s);
}
because the for loop uses an iterator and while iterating the list is changed.
Avoiding this is easy just use a normal index based for loop.
int l=list.size ();
for (int i=0;i <l;i++){
String s=list.get (i)
if (s.equals (""){
list.remove (s);
l--;}
}