Comparing two hashmaps for equal values and same key sets?(比较两个哈希图是否具有相同的值和相同的键集?)
问题描述
我怎样才能最好地比较两个 HashMap
,如果我想知道它们是否包含不同的键,以及这些键的值是否相互匹配.
How can I best compare two HashMap
s, 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 HashMap
s.
注意:
如果您的 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()
.
这篇关于比较两个哈希图是否具有相同的值和相同的键集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:比较两个哈希图是否具有相同的值和相同的键集


基础教程推荐
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01