比较两个相等值和相同键集合的哈希值? [英] Comparing two hashmaps for equal values and same key sets?

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

问题描述

如果我想知道它们中是否有不同的键,如果这些键的值匹配,我如何最好比较两个 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 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");

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 HashMaps.

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屋!

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