比较相等值和相同键集的两个哈希图? [英] 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 不同的键,应该会失败.
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
检查s.
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()
.
这篇关于比较相等值和相同键集的两个哈希图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!