Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing HashMaps in Java

I have two HashMaps: FOO & BAR.

HashMap FOO is a superset of HashMap BAR.

How do I find out what 'keys' are missing in HashMap BAR (i.e. exists in FOO but not BAR)?

like image 871
user353829 Avatar asked Feb 18 '10 23:02

user353829


2 Answers

Set missing = new HashSet(foo.keySet());
missing.removeAll(bar.keySet());
like image 167
erickson Avatar answered Oct 14 '22 07:10

erickson


If you're using google-collections (and realistically I think it should be on the classpath of more or less every non-trivial Java project) it's just:

Set<X> missing = Sets.difference(foo.keySet(), bar.keySet();
like image 28
Cowan Avatar answered Oct 14 '22 08:10

Cowan