Ankush Kapoor
Ankush Kapoor

Reputation: 543

How to Compare 2 LinkedHashMap and get difference in keys & values?

There are 2 LinkedHashMaps. I have to check if both LinkedHashMaps have same keys & values. If one LinkedHashMap contains extra keys + values then it should get printed or stored in 3rd LinkedHashMap. If any of the key or value is different in 2nd LinkedHashMap then it should be notified (print in console)

Eg

Map1 contains {A-a, B-b, C-c}

Map2 contains {A-a, B-r, C-c, Z-z}

So here 3rd LinkedHashMap should contain {Z-z} as it contains extra key and

{B-r} should get printed as it is modified

Upvotes: 0

Views: 1746

Answers (1)

NiloufarMP
NiloufarMP

Reputation: 56

you can use this class :)

public class MapCompare {
    private LinkedHashMap<String, String> differencesMap;
    private LinkedHashMap<String, String> extraMap;

    private void main(LinkedHashMap<String, String> map1, LinkedHashMap<String, String> map2) {
        this.differencesMap = new LinkedHashMap<>();
        this.extraMap = new LinkedHashMap<>();

        Set<Map.Entry<String, String>> set1 = ((LinkedHashMap<String, String>) map1.clone()).entrySet();
        Set<Map.Entry<String, String>> set2 = ((LinkedHashMap<String, String>) map2.clone()).entrySet();

        set1.removeAll(set2);
        compare(set1, map2);

        set1 = map1.entrySet();

        set2.removeAll(set1);
        compare(set2, map1);
    }

    private LinkedHashMap<String, String> getDifferencesMap() {
        return differencesMap;
    }

    private LinkedHashMap<String, String> getExtraMap() {
        return extraMap;
    }

    private void compare(Set<Map.Entry<String, String>> set, LinkedHashMap<String, String> map) {
        for (Map.Entry<String, String> entry : set) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (map.containsKey(key) || map.containsValue(value)) {
                differencesMap.put(key, value);
            } else {
                extraMap.put(key, value);
            }
        }
    }

    public static void main(String[] args) {
        LinkedHashMap<String, String> map1 = new LinkedHashMap<>();
        map1.put("A", "a");
        map1.put("B", "b");
        map1.put("C", "c");
        LinkedHashMap<String, String> map2 = new LinkedHashMap<>();
        map2.put("A", "a");
        map2.put("C", "c");
        map2.put("B", "r");
        map2.put("Z", "z");

        MapCompare mapCompare = new MapCompare();
        mapCompare.main(map1,map2);

        System.out.println("diff: " + mapCompare.getDifferencesMap());
        System.out.println("extra: " + mapCompare.getExtraMap());
    }
}

Upvotes: 2

Related Questions