Remove a key in hashmap when the value's hashs

2019-04-28 17:51发布

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...
}

3条回答
乱世女痞
2楼-- · 2019-04-28 18:26
 Iterator<String> iterator = mMapFiles.keySet().iterator();
    while (iterator.hasNext()){
        if ( mMapFiles.get( iterator.next() ).size() < 1 )
            iterator.remove();
    }
查看更多
太酷不给撩
3楼-- · 2019-04-28 18:29

Since Java 8, there is an excelent short solution with lambda:

stringIDMap.entrySet().removeIf(ent -> ent.getValue().isEmpty());

or you can use more concise way by passing method reference

stringIDMap.values().removeIf(Set::isEmpty);

查看更多
叼着烟拽天下
4楼-- · 2019-04-28 18:36

In order to avoid ConcurrentModificationException, you need to use the Iterator interface directly:

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 that you are attempting to remove elements from the map while iterating over it. When you call stringIDMap.remove(), this invalidates the iterator that the for-each loop uses under the cover, making further iteration impossible.

it.remove() solves this problem as it does not invalidate the iterator.

查看更多
登录 后发表回答