user353829
user353829

Reputation: 1434

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)?

Upvotes: 6

Views: 1727

Answers (2)

Cowan
Cowan

Reputation: 37533

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();

Upvotes: 7

erickson
erickson

Reputation: 269627

Set missing = new HashSet(foo.keySet());
missing.removeAll(bar.keySet());

Upvotes: 13

Related Questions