我如何断言两个带有 Javabean 值的 HashMap 是相等的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18627494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I assert that two HashMap with Javabean values are equal?
提问by Ayrx
I have two HashMap<Integer, Question>
maps that I would like to compare. Question
in this case is a Javabean I have written.
我有两张HashMap<Integer, Question>
地图要比较。Question
在这种情况下是我编写的 Javabean。
How do I assert that both HashMap
are equal? In this scenario, equal means that both HashMap
contains exactly the same Question
bean?
我如何断言两者HashMap
是平等的?在这种情况下,equal 意味着两者HashMap
包含完全相同的Question
bean?
If it's at all relevant, I am writing a unit test using JUnit.
如果它完全相关,我正在使用 JUnit 编写单元测试。
采纳答案by Ayrx
Here is the solution I eventually ended up using which worked perfectly for unit testing purposes.
这是我最终使用的解决方案,它非常适合单元测试目的。
for(Map.Entry<Integer, Question> entry : questionMap.entrySet()) {
assertReflectionEquals(entry.getValue(), expectedQuestionMap.get(entry.getKey()), ReflectionComparatorMode.LENIENT_ORDER);
}
This involves invoking assertReflectionEquals()
from the unitils
package.
这涉及assertReflectionEquals()
从unitils
包中调用。
<dependency>
<groupId>org.unitils</groupId>
<artifactId>unitils-core</artifactId>
<version>3.3</version>
<scope>test</scope>
</dependency>
回答by Thilo
If your Question class implements equals
then you can just do
如果你的 Question 类实现了,equals
那么你就可以做
assertEquals(expectedMap, hashMap);
assertTrue(expectedMap.equals(hashMap));
The Map interface specifies that two Maps are equal if they contain equal elements for equal keys.
Map 接口指定如果两个 Map 包含相同键的相同元素,则它们是相等的。
回答by TheGraduateGuy
if your bean class has a field which will be unique for every object you create then you should override equals and hashcode method in ur bean class with that field
如果您的 bean 类有一个字段,该字段对于您创建的每个对象都是唯一的,那么您应该使用该字段覆盖您的 bean 类中的 equals 和 hashcode 方法
回答by sameer_mishra
Too compare maps ,in your particular case :
太比较地图,在您的特定情况下:
1)Check the size of the map if its equal
1)检查地图的大小是否相等
Then use
然后使用
`assertTrue(expectedMap.equals(hashMap));`
In your Question bean you have to override the equals and hashcode method.
在您的问题 bean 中,您必须覆盖 equals 和 hashcode 方法。
回答by Sachin Thapa
Here is how HashMap equal method works:
下面是 HashMap equal 方法的工作原理:
public boolean equals(Object o) {
..........
..........
Map<K,V> m = (Map<K,V>) o;
..........
Iterator<Entry<K,V>> i = entrySet().iterator();
while (i.hasNext()) {
Entry<K,V> e = i.next();
K key = e.getKey();
V value = e.getValue();
if (value == null) {
if (!(m.get(key)==null && m.containsKey(key)))
return false;
} else {
if (!value.equals(m.get(key)))
return false;
}
...........
...........
return true;
}
Now since, it is invoking the equals method of Value objects, that means Value objects for a given key should be same (as governed by equals method).
现在,它调用 Value 对象的 equals 方法,这意味着给定键的 Value 对象应该相同(由 equals 方法控制)。
Above will help you to understand in what case your JUnit will pass. In your JUnit method you can use:
以上将帮助您了解您的 JUnit 在什么情况下会通过。在您的 JUnit 方法中,您可以使用:
public static void assertEquals(java.lang.Object expected,
java.lang.Object actual)
See linkfor more details.
有关更多详细信息,请参阅链接。
Cheers !!
干杯!!
回答by Shailesh Saxena
You can use compare(-,-)
(Mostly used for sorting
purpose) of Comparator
interface to implement custom comparison between objects as your requirement. Or use equals()
method to compare objects.
您可以根据需要使用compare(-,-)
(主要用于sorting
目的)Comparator
接口来实现对象之间的自定义比较。或者使用equals()
方法比较对象。
回答by Ariel T
Using Guava you can do:
使用番石榴,您可以:
assertTrue(Maps.difference(expected, actual).areEqual());
回答by Maria Charles
I found the keySetmethod of the map class useful
我发现地图类的keySet方法很有用
assertEquals(map1.keySet(), map2.keySet());