0

我正在尝试比较哈希映射的键值对,其中值顺序与我从一个源读取 map1 和从另一个源读取 map2 不同。键将相同,但订单列表将采用不同的顺序。

比较导致失败,我需要修复它

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

    public class TestClass {
        private static Map<String, List<String>> tempMap;
        private static Map<String, List<String>> tempMap1;
        private static Map<String, List<String>> tempMap2;
        private static Map<String, List<String>> tempMap3;

        public static Map<String, List<String>> practiceMap(){
            tempMap = new HashMap<String, List<String>>();
            List<String> completeDateKey = new ArrayList<>();
            List<String> statusKey = new ArrayList<String>();
            List<String> completeDateValue = new ArrayList<String>();
            List<String> statusValue = new ArrayList<String>();

            ArrayList<String> key = new ArrayList<String>(Arrays.asList("Complete Date", "Status"));
            ArrayList<String> completedate = new ArrayList<String>(Arrays.asList("", "11/15/2019"));
            ArrayList<String> status = new ArrayList<String>(Arrays.asList("NEW", "CLOSE"));

            completeDateKey.add(key.get(0));
            statusKey.add(key.get(1));
            completeDateValue.addAll(completedate);
            statusValue.addAll(status);

            tempMap.put(completeDateKey.toString(), completeDateValue);
            tempMap.put(statusKey.toString(), statusValue);

            return tempMap;
        }


        public static Map<String, List<String>> practiceMap1(){
            tempMap1 = new HashMap<String, List<String>>();
            List<String> completeDateKey = new ArrayList<>();
            List<String> statusKey = new ArrayList<String>();
            List<String> completeDateValue = new ArrayList<String>();
            List<String> statusValue = new ArrayList<String>();

            ArrayList<String> key = new ArrayList<String>(Arrays.asList("Complete Date", "Status"));
            ArrayList<String> completedate = new ArrayList<String>(Arrays.asList("11/15/2019", ""));
            ArrayList<String> status = new ArrayList<String>(Arrays.asList("CLOSE", "NEW"));

            completeDateKey.add(key.get(0));
            statusKey.add(key.get(1));
            completeDateValue.addAll(completedate);
            statusValue.addAll(status);
            tempMap1.put(completeDateKey.toString(), completeDateValue);
            tempMap1.put(statusKey.toString(), statusValue);
            return tempMap1;
        }

        public static void comparisionTest() {
            tempMap3 = new HashMap<String, List<String>>();
            tempMap2 = new HashMap<String, List<String>>();
            tempMap2 = practiceMap();
            tempMap3 = practiceMap1();
            System.out.println("map 1 is " + tempMap2);
            System.out.println("map 2 is " + tempMap3);
            int size = practiceMap().size();
            System.out.println(size);

              if(tempMap2.equals(tempMap3) == true) { System.out.println("Successful"); }
              else { System.out.println("failed"); }

        }

        public static void main(String[] args) {
            new TestClass().comparisionTest();
        }

    }

我该如何解决这个问题,以便我的比较成功

4

2 回答 2

1

您正在寻找的东西称为数据规范化(广义上)。

含义:当您想以有意义的方式比较数据时,您必须确保您查看的所有数据都以相同的方式“组织”。

在您的情况下:如果“相等”意味着:相同的密钥对和相同的内容(忽略顺序),那么您可以轻松地将其标准化,例如:

  • 对哈希键进行排序(当您按字典顺序迭代哈希键时,您有一个定义的顺序)
  • 对列表进行排序

您可以“即时”执行此操作Comparator(例如在实现中),也可以将您的数据显式复制到此类“有序”集合中。

于 2020-01-01T17:44:06.643 回答
0

如果您的列表中没有重复项,您可以使用集合,因为相同值的集合比较相等,不考虑顺序。

            Set<String> set1 = new LinkedHashSet<>();
            set1.add("Alpha");
            set1.add("Beta");
            set1.add("Gamma");
            Set<String> set2 = new LinkedHashSet<>();
            set2.add("Gamma");
            set2.add("Beta");
            set2.add("Alpha");

            Map<String, Set<String>> map1 = Map.of("A", set1);
            Map<String, Set<String>> map2 = Map.of("A", set2);

            System.out.println(set1.equals(set2)); // true
            System.out.println(map1.equals(map2)); // true
于 2020-01-01T20:34:42.847 回答