Remove a key in hashmap when the value's hashset is Empty
问题 I have a hashmap that maps strings keys to hashsets values, and I want to remove a key from the hashmap when the hashmaps's hashset value is empty. I'm having trouble approaching this. Here's what I've tried but I'm very stuck: for(Map.Entry<String, HashSet<Integer>> entr : stringIDMap.entrySet()) { String key = entr.getKey(); if (stringIDMap.get(key).isEmpty()) { stringIDMap.remove(key); continue; } //few print statements... } 回答1: In order to avoid ConcurrentModificationException, you need