Jan 30

The general cause of a ConcurrentModificationException is from trying to remove an instance from a collection that is being iterated over as in the following example.


Iterator i = mylist.iterator();
while (i.hasNext())
{
   Object o = i.next();
   if (something)
   {
       // following line will throw ConcurrentModificationException
       mylist.remove(o);
   }
}

The correct way is to instead use the Iterator’s remove() method as shown in the following snippet:


Iterator i = mylist.iterator();
while (i.hasNext())
{
   Object o = i.next();
   if (something)
   {
       i.remove();
   }
}

written by objects \\ tags: , ,