ConcurrentModificationException on isEmpty() method of ArrayList

Viewed 725

I have the following code:

Map<String, List<String>> map;
for(String k : map.keySet()){
   List<String> list = map.get(k);
   boolean empty = list.isEmpty();//CME
   if(!empty && somecheck(k, ...)){
      list.clear();
   }
}

And I'm getting ConcurrentModificationException in isEmpty() method. List is an ArrayList. There is no other threads modifying list, because it was created in this method before (and the all map too). The only place modifying list is clear(), but it called after isEmpty() and loop cannot execute on one list twice.

I'm using java 1.7

java.util.ConcurrentModificationException
    at java.util.ArrayList$SubList.checkForComodification(ArrayList.java:1169)
    at java.util.ArrayList$SubList.size(ArrayList.java:998)
    at java.util.AbstractCollection.isEmpty(AbstractCollection.java:86)
1 Answers
Related