比较物体地图

时间:2012-08-13 15:21:16

标签: java collections map

我已经设置了一个测试:

  • 检索有关多个法庭案件的数据:每个法庭案件都存储在CourtCase对象中
  • 然后将一组CourtCase对象存储在Map
  • 我检索了两次这些数据(来自两个不同的来源),所以我最终得到了两张地图

地图中对象内的数据应该相同,但地图中对象的顺序可能不是:

地图1: A,case1 - B,case2 - C,case3

地图2: B,case2 - A,case1 - C,case3

如何才能最好地比较这两张地图?

3 个答案:

答案 0 :(得分:3)

Map#equals不关心订单。只要您的2个地图包含相同的映射,它就会返回true。

Map#equals使用Set#equals方法,应用于条目集。 Set#equals contract

  

如果指定的对象也是一个集合,则返回true,两个集合具有相同的大小,并且指定集合的​​每个成员都包含在此集合中(或者等效地,此集合的每个成员都包含在指定的集合中)

注意:这假设您的CourtCase个对象具有适当的equalshashcode方法进行比较。

答案 1 :(得分:0)

Map实现提供了一个equals方法来完成这个技巧。 Map.equals

答案 2 :(得分:0)

@ user973718最好比较java中的两个地图对象 - 你可以将地图的键添加到列表中,使用这两个列表你可以使用方法retainAll()和removeAll()并将它们添加到另一个公共键列表和不同的键列表。使用公共列表和不同列表的键可以迭代地图,使用等于你可以比较地图。

以下代码给出了这个输出: 在{b = 2,c = 3,a = 1}之前 在{c = 333,a = 1}之后 不平等:之前 - 3之后 - 333 平等:之前 - 1之后 - 1 值仅出现在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;
    }
}

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; } }