Compare hashmaps in Java

I have two hashmaps: foo & bar

HashMap foo is a superset of HashMap bar

How to find out the missing "key" in HashMap bar (i.e. it exists in foo but not in bar)?

Solution

If you are using Google collections (and actually I think it should be on the classpath of more or less every non trivial java project), it just:

Set<X> missing = Sets.difference(foo.keySet(),bar.keySet();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>