Removing entries from a HashMap - DZone Java
an java.util.ConcurrentModificationException will occur!Solving this issue involves adding an Iterator which conforming to documentation "Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics." will allows us to call remove(). Therefore, here it is:
for(Iterator<Map.Entry<String,Integer>>it=map.entrySet().iterator();it.hasNext();){ Map.Entry<String, Integer> entry = it.next(); if (entry.getValue() > 5) { it.remove(); } }Done !Read full article from Removing entries from a HashMap - DZone Java
No comments:
Post a Comment