'Compare 2 nearly identical maps and find key of value that differate [duplicate]

So I have two identical maps with String as key and Integer as value and I know that after certain event only 1 value in map2 increment by 1.

Map<String, Integer> map1 = new HashMap<>();
map1.put("First", 1)
map1.put("Second", 2)
map1.put("Third", 3)
Map<String, Integer> map2 = new HashMap<>();
map2.put("First", 1)
map2.put("Second", 3)
map2.put("Third", 3)

So my question is how I can return "Second" key because its value is incremented by 1.



Solution 1:[1]

The stream pipeline in your answer works, but might be more complex than necessary. Essentially it's performing a difference operation on the sets of entries of each map. You can do this using the collections bulk operation removeAll instead of a stream, like so:

// setup
var map1 = Map.of("First", 1, "Second", 2, "Third", 3);
var map2 = Map.of("First", 1, "Second", 3, "Third", 3);

// compute difference of entry sets
var difference = new HashMap<>(map1);
difference.entrySet().removeAll(map2.entrySet());

difference ==> {Second=2}

Solution 2:[2]

It seems you just want to get the keys and without the values, so the following method will do exactly that.

public static <K> List mapDifference(Map<? extends K, ? extends Object> map1, Map<? extends K, ? extends Object> map2)
{
        List<K> keys = new ArrayList<K>();
        Set<Map.Entry<? extends K, ? extends Object>> set;
        Map<? extends K, ? extends Object> other;
        if(map1.size() > map2.size()) {
            set = map1.entrySet(); other = map2;
        } else {
            set = map2.entrySet(); other = map1;
        }
        Iterator<Map.Entry<? extends K, ? extends Object>> iterator = set.iterator();
        while(iterator.hasNext()) {
            Map.Entry<? extends K, ?  extends Object> entry = iterator.next();
            K key = entry.getKey();
            if(!other.containsKey(key) || !other.get(key).equals(entry.getValue())) {
                keys.add(key);
            }
        }
        return keys;
}

TESTS

Map<String, Integer> map1 = new HashMap<>();
map1.put("First", 1);
map1.put("Second", 2);
map1.put("Third", 3);
Map<String, Integer> map2 = new HashMap<>();
map2.put("First", 1);
map2.put("Second", 3);
map2.put("Third", 3);

List<String> diff = mapDifference(map1, map2);
System.out.println(diff);
[Second]
Map<String, Integer> map1 = new HashMap<>();
map1.put("First", 1);
map1.put("Second", 2);
map1.put("Third", 3);
Map<String, Integer> map2 = new HashMap<>();
map2.put("First", 1);
map2.put("Second", 3);
map2.put("Third", 3);
map2.put("Forth", 4);

List<String> diff = mapDifference(map1, map2);
System.out.println(diff);
[Second, Forth]

Solution 3:[3]

Map<String, Integer> difference = map1.entrySet().stream()
    .filter(entry -> !entry.getValue().equals(map2.get(entry.getKey())))
    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stuart Marks
Solution 2 Darkman
Solution 3 Lino