Comparing two hashmaps for equal values and same key sets?(比较两个哈希图是否具有相同的值和相同的键集?)
问题描述
我怎样才能最好地比较两个 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?
推荐答案
对两个 HashMap 的 keySet() 进行 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().
这篇关于比较两个哈希图是否具有相同的值和相同的键集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:比较两个哈希图是否具有相同的值和相同的键集
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
