比较两个哈希图是否具有相同的值和相同的键集? [英] Comparing two hashmaps for equal values and same key sets?

查看:23
本文介绍了比较两个哈希图是否具有相同的值和相同的键集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能最好地比较两个 HashMap,如果我想知道它们是否包含不同的键,以及这些键的值是否相互匹配.

How can I best compare two HashMaps, if I want to find out if none of them contains different keys than the other, and if the values of that keys match each other.

Map<objA, objB> mapA = new HashMap<objA, objB>();
mapA.put("A", "1");
mapA.put("B", "2");

Map<objA, objB> mapB = new HashMap<objA, objB>();
mapB.put("D", "4");
mapB.put("A", "1");

比较A和B时,应该会因为B和D的key不同而失败.

When comparing A with B, it should fail due to different keys B and D.

如何最好地比较未排序的哈希图?

How could I best compare non-sorted hashmaps?

推荐答案

对两个 HashMapkeySet() 进行 equals 检查秒.

Make an equals check on the keySet() of both HashMaps.

注意:

如果您的 Map 包含 String 键,那没问题,但如果您的 Map 包含 objA 类型键,那么您需要确保你的类 objA 实现了 equals().

If your Map contains String keys then it is no problem, but if your Map contains objA type keys then you need to make sure that your class objA implements equals().

这篇关于比较两个哈希图是否具有相同的值和相同的键集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆