How to remove elements of one map from another map?

dictionary, hashmap, java

Solution

You can try:

foo.keySet().removeAll(baar.keySet())

Changes to a `Map`'s `keySet()` are reflected in the map itself.

If you want to remove exact mappings (not just based on keys), you can use the same approach with the `entrySet()` instead:

foo.entrySet().removeAll(baar.entrySet());

Problem

``` HashMap<String, String> foo = new HashMap<String, String>(); HashMap<String, String> baar = new HashMap<String, String>(); ``` How to remove items found in `baar` from `foo`?

Original source