If you get a ConcurrentModificationException, you are likely to have multiple threads.
Thus, the complete answer includes both using Iterator.remove () and synchronizing access to the collection.
( , ):
synchronized ( lock ) {
List<Apple> apples = appleCart.getApples();
for ( Iterator<Apple> it = apples.iterator(); it.hasNext(); )
{
Apple a = it.next();
if ( a.hasWorm() ) {
it.remove();
}
}
}