比较两个相等值和相同键集合的哈希值? [英] Comparing two hashmaps for equal values and same key sets?
问题描述
如果我想知道它们中是否有不同的键,如果这些键的值匹配,我如何最好比较两个 HashMap
彼此。
地图< objA,objB> mapA = new HashMap< objA,objB>();
mapA.put(A,1);
mapA.put(B,2);
映射< objA,objB> mapB = new HashMap< objA,objB>();
mapB.put(D,4);
mapB.put(A,1);
当比较A和B时,由于不同的键B和D,它应该会失败。
如何更好地比较非排序hashmaps?
c $ c> equals 检查 HashMap
的 keySet()
/ p>
注意:
$ c> Map 包含 String
键,那么没有问题,但是如果你的Map包含 objA
type键,那么你需要确保你的类 objA
实现 equals()
。
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");
When comparing A with B, it should fail due to different keys B and D.
How could I best compare non-sorted hashmaps?
Make an equals
check on the keySet()
of both HashMap
s.
NOTE:
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屋!