开发者

Comparing HashMaps in Java

开发者 https://www.devze.com 2022-12-20 11:17 出处:网络
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 B开发者_如何转开发AR (i.e. exists in FOO but not BAR)?Set missing

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 B开发者_如何转开发AR (i.e. exists in FOO but not BAR)?


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


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

精彩评论

暂无评论...
验证码 换一张
取 消