I want to iterate a HashMap like:
for (Map.Entry<Integer, Integer> entry : map.entrySet()) { map.replace(entry.getKey(), entry.getValue()-1); if (entry.getValue() == 0) { map.remove(entry.getKey(), 0); } } This ends in an Exception: java.util.ConcurrentModificationException
Any solutions?
Kind regards
removeoperation, or through thesetValueoperation on a map entry returned by the iterator) the results of the iteration are undefined." docs.oracle.com/javase/8/docs/api/java/util/Map.html#entrySet-- Here, "undefined" means to throw a CME.