0

私は次のようなテストを設定しました。

  • いくつかの訴訟に関するデータを取得します。各訴訟はCourtCaseオブジェクトに保存されます。
  • その後、CourtCaseオブジェクトのセットがマップに保存されます
  • これらのデータを(2つの異なるソースから)2回取得するため、2つのマップになります

オブジェクト内のデータはマップ間で同じである必要がありますが、マップ内のオブジェクトの順序は次のようになっていない場合があります。

マップ1:A、case1-B、case2-C、case3

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

これら2つのマップを最もよく比較するにはどうすればよいですか?

4

3 に答える 3

3

Map#equals順序は気にしません。2 つのマップに同じマッピングが含まれている限り、true が返されます。

Map#equalsメソッドを使用Set#equalsし、エントリ セットに適用されます。Set#equals契約:

指定されたオブジェクトもセットであり、2 つのセットが同じサイズであり、指定されたセットのすべてのメンバーがこのセットに含まれている (または、このセットのすべてのメンバーが指定されたセットに含まれている) 場合は、true を返します。

CourtCase注: これは、オブジェクトに比較する適切なメソッドがあることequalsを前提としています。hashcode

于 2012-08-13T15:26:30.990 に答える
0

Map の実装は、トリックを行う equals メソッドを提供します。Map.equals

于 2012-08-13T15:25:24.797 に答える
0

@ user973718 Javaで2つのマップオブジェクトを比較するのに最適なのは、マップのキーをリストに追加でき、これら2つのリストでretainAll()およびremoveAll()メソッドを使用して、それらを別の共通キーリストに追加し、異なるキーリスト。共通リストと異なるリストのキーを使用してマップを反復処理し、equals を使用してマップを比較できます。

以下のコードは、この出力を提供します: 前 {b=2, c=3, a=1} 後 {c=333, a=1} 不等: 前- 3 後- 333 等しい: 前- 1 後- 1 存在する値のみマップ前: 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;
    }
}

于 2012-12-10T12:45:46.470 に答える