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

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

问题描述

如何最好地比较两个 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 不同的键,应该会失败.

When comparing A with B, it should fail due to different keys B and D.

我怎样才能最好地比较未排序的哈希图?

How could I best compare non-sorted hashmaps?

推荐答案

HashMapkeySet() 进行 equals 检查s.

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().

这篇关于比较相等值和相同键集的两个哈希图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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