Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove elements of one map from another map?

Tags:

java

hashmap

map

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?

like image 292
Max_Salah Avatar asked Aug 07 '13 13:08

Max_Salah


1 Answers

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());
like image 85
arshajii Avatar answered Oct 07 '22 19:10

arshajii