To avoid ConcurrentModificationException, you need to use the interface directly Iterator:
Iterator<Map.Entry<String, HashSet<Integer>>> it = stringIDMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, HashSet<Integer>> e = it.next();
String key = e.getKey();
HashSet<Integer> value = e.getValue();
if (value.isEmpty()) {
it.remove();
}
}
The reason your current code doesn't work is because you are trying to remove items from the map while iterating over it. When you call stringIDMap.remove(), it invalidates the iterator that is used for each loop under the cover, making further iteration impossible.
it.remove() , .