ConcurrentModificationException for isEmpty () method of ArrayList method

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 get ConcurrentModificationExceptionin the isEmpty () method. The list is this ArrayList. There is no other list of thread changes because it was created in this method before (and the whole map too). The only modified place list is clear(), but it is called after isEmpty (), and the loop cannot run twice in the same list.

I am 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)
+4
source share
1 answer

An exception is thrown from the stacktrace you provided in a subclass that implements SubList functionality. I suppose the lists on your map are actually subscriptions of another list?

, , , , (, - - ).

, , , :

map.put(key, new ArrayList(originalList.subList(start, end));
+3

Source: https://habr.com/ru/post/1678247/


All Articles