Java |比较四个HashMaps

时间:2017-06-16 13:24:26

标签: java hashmap compare size

我想问你,如何比较java中的四个HashMaps大小。 如果所有HashMaps中都有> = 2个键,如果它们在同一个中,我怎么能找到... 感谢。

1 个答案:

答案 0 :(得分:1)

您可以检查哈希图的键交叉长度:

Set<String> commonKeys = new HashSet<>(hashMap1.keySet());
commonKeys.retainAll(hashMap2.keySet());
commonKeys.retainAll(hashMap3.keySet());
commonKeys.retainAll(hashMap4.keySet());
commonKeys.size();

您应该将commonKeys Set的类型参数调整为HashMap s&#39;的类型。密钥:处理HashMap<Integer, Whatever>时,您需要Set<Integer>