iterating over and removing from a map

java

Solution

As of Java 8 you could do this as follows:

map.entrySet().removeIf(e -> <boolean expression>);

Oracle Docs: `entrySet()`

The set is backed by the map, so changes to the map are reflected in the set, and vice-versa

Problem

I was doing: ``` for (Object key : map.keySet()) if (something) map.remove(key); ``` which threw a ConcurrentModificationException, so i changed it to: ``` for (Object key : new ArrayList<Object>(map.keySet())) if (something) map.remove(key); ``` this, and any other procedures that modify the map are in synchronized blocks. is there a better solution?

Original source

Related problems