0

I am trying to compare the Key value pairs of an hash map where the value order is not same as i am reading the map1 from one source and map2 from another source. The keys will be same but the order list will be in a different order.

The comparison is resulting fail, which i need to fix it

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

    }

How can i fix this so my comparision is successful

Anil Kumar
  • 23
  • 2

2 Answers2

1

What you are looking for is called data normalization (in a broad sense).

Meaning: when you want to compare data in meaningful ways, you have to ensure that all data you look at is "organized" the same way.

In your case: if "equal" means: the same pairs of keys, and same content (ignoring order), then you can easily normalized that, for example by:

  • sorting the hash keys (when you iterate the hash keys in lexicographical order, well, you have a defined order)
  • sorting the lists

You could either do that "on the fly" (within a Comparator implementation for example), or by explicitly copying your data into such "ordered" collections.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Sorry i am completely new to the topic, i am totally blown over now. Yeah i wanted to compare the content in the ignoring the order. How can i achieve "On the fly" Comparator if had to do it that way – Anil Kumar Jan 01 '20 at 18:03
  • 1
    @AnilKumar That is the essence of learning programming ... sitting down and doing research. You start by reading https://stackoverflow.com/questions/2839137/how-to-use-comparator-in-java-to-sort for example. **Learn** to use a comparator with small examples. Then work yourself up to larger data. – GhostCat Jan 01 '20 at 18:07
0

If your lists do not have duplicates in them you could use sets since sets of the same values compare equally without regard to order.

            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
WJS
  • 36,363
  • 4
  • 24
  • 39