'How do I assert that two HashMap with Javabean values are equal?

I have two HashMap<Integer, Question> maps that I would like to compare. Question in this case is a Javabean I have written.

How do I assert that both HashMap are equal? In this scenario, equal means that both HashMap contains exactly the same Question bean?

If it's at all relevant, I am writing a unit test using JUnit.



Solution 1:[1]

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.

<dependency>
    <groupId>org.unitils</groupId>
    <artifactId>unitils-core</artifactId>
    <version>3.3</version>
    <scope>test</scope>
</dependency>

Solution 2:[2]

Using Guava you can do:

assertTrue(Maps.difference(expected, actual).areEqual());

Solution 3:[3]

If your Question class implements equals then you can just do

assertEquals(expectedMap, hashMap);

assertTrue(expectedMap.equals(hashMap));

The Map interface specifies that two Maps are equal if they contain equal elements for equal keys.

Solution 4:[4]

Too compare maps ,in your particular case :

1)Check the size of the map if its equal

Then use

    `assertTrue(expectedMap.equals(hashMap));` 

In your Question bean you have to override the equals and hashcode method.

Solution 5:[5]

Here is how HashMap equal method works:

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

Above will help you to understand in what case your JUnit will pass. In your JUnit method you can use:

public static void assertEquals(java.lang.Object expected,
                                java.lang.Object actual)

See link for more details.

Cheers !!

Solution 6:[6]

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

Solution 7:[7]

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.

Solution 8:[8]

I found the keySet method of the map class useful

        assertEquals(map1.keySet(), map2.keySet());

Solution 9:[9]

 assertEquals(expected.toString(), actual.toString());

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ayrx
Solution 2 Ariel T
Solution 3 Thilo
Solution 4 Community
Solution 5 Sachin Thapa
Solution 6 TheGraduateGuy
Solution 7 Shailesh Saxena
Solution 8 Charles
Solution 9 David Urry