0

I have set up a test that:

  • retrieves data concerning several court cases: each court case is stored in a CourtCase object
  • a set of CourtCase objects is then stored in a Map
  • I retrieve these data twice (from two different sources) so I end up with two Maps

The data within the objects should be the same between the Maps, but the order of the objects within the Maps may not be:

Map1: A, case1 - B, case2 - C, case3

Map2: B, case2 - A, case1 - C, case3

How can I best compare these two Maps?

user973718
  • 225
  • 2
  • 5
  • 16

3 Answers3

3

Map#equals does not care about the order. As long as your 2 maps contain the same mapping it will return true.

Map#equals uses Set#equals method, applied to the entry set. Set#equals contract:

Returns true if the specified object is also a set, the two sets have the same size, and every member of the specified set is contained in this set (or equivalently, every member of this set is contained in the specified set).

Note: this assumes that your CourtCase objects have proper equals and hashcode methods to be compared.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • ... and as long as `CourtCase` has proper implementation of `equals()` and `hashCode()`. – Philipp Reichart Aug 13 '12 at 15:27
  • @PhilippReichart Yes - I assumed it was the case and only the order was the issue but you are right that it is a necessary condition. – assylias Aug 13 '12 at 15:28
  • This java newb thanks you for the clear answer and most of all for the heads-up on the hashCode() and equals() methods; I have overridden those now in the CourtCase class. – user973718 Aug 13 '12 at 17:04
0

Map implementations provides an equals method which do the trick. Map.equals

gontard
  • 28,720
  • 11
  • 94
  • 117
0

@user973718 the best to compare two map objects in java is - you can add the keys of a map to list and with those 2 lists you can use the methods retainAll() and removeAll() and add them to another common keys list and different keys list. Using the keys of the common list and different list you can iterate through map, using equals you can compare the maps.

The below code gives this output : Before {b=2, c=3, a=1} After {c=333, a=1} Unequal: Before- 3 After- 333 Equal: Before- 1 After- 1 Values present only in before map: 2

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;

public class Demo 
{
    public static void main(String[] args) 
    {
        Map<String, String> beforeMap = new HashMap<String, String>();
        beforeMap.put("a", "1");
        beforeMap.put("b", "2");
        beforeMap.put("c", "3");

        Map<String, String> afterMap = new HashMap<String, String>();
        afterMap.put("a", "1");
        afterMap.put("c", "333");

        System.out.println("Before "+beforeMap);
        System.out.println("After "+afterMap);

        List<String> beforeList = getAllKeys(beforeMap);

        List<String> afterList = getAllKeys(afterMap);

        List<String> commonList1 = beforeList;
        List<String> commonList2 = afterList;
        List<String> diffList1 = getAllKeys(beforeMap);
        List<String> diffList2 = getAllKeys(afterMap);

        commonList1.retainAll(afterList);
        commonList2.retainAll(beforeList);

        diffList1.removeAll(commonList1);
        diffList2.removeAll(commonList2);

        if(commonList1!=null & commonList2!=null) // athough both the size are same
        {
            for (int i = 0; i < commonList1.size(); i++) 
            {
                if ((beforeMap.get(commonList1.get(i))).equals(afterMap.get(commonList1.get(i)))) 
                {
                    System.out.println("Equal: Before- "+ beforeMap.get(commonList1.get(i))+" After- "+afterMap.get(commonList1.get(i)));
                }
                else
                {
                    System.out.println("Unequal: Before- "+ beforeMap.get(commonList1.get(i))+" After- "+afterMap.get(commonList1.get(i)));
                }
            }
        }
        if (CollectionUtils.isNotEmpty(diffList1)) 
        {
            for (int i = 0; i < diffList1.size(); i++) 
            {
                System.out.println("Values present only in before map: "+beforeMap.get(diffList1.get(i)));
            }
        }
        if (CollectionUtils.isNotEmpty(diffList2)) 
        {
            for (int i = 0; i < diffList2.size(); i++) 
            {
                System.out.println("Values present only in after map: "+afterMap.get(diffList2.get(i)));
            }
        }
    }

    /**getAllKeys API adds the keys of the map to a list */
    private static List<String> getAllKeys(Map<String, String> map1)
    {
        List<String> key = new ArrayList<String>();
        if (map1 != null) 
        {
            Iterator<String> mapIterator = map1.keySet().iterator();
            while (mapIterator.hasNext()) 
            {
                key.add(mapIterator.next());
            }
        }
        return key;
    }
}

tinker_fairy
  • 1,323
  • 1
  • 15
  • 18