How to receive difference of maps in java?

28,552

Solution 1

How about google guava?:

Maps.difference(map1,map2)

Solution 2

Here is a simple snippet you can use instead of massive Guava library:

public static <K, V> Map<K, V> mapDifference(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right) {
    Map<K, V> difference = new HashMap<>();
    difference.putAll(left);
    difference.putAll(right);
    difference.entrySet().removeAll(right.entrySet());
    return difference;
}

Check out the whole working example

Solution 3

If I understood well you are trying to calculate symmetric difference beetween the two maps entry sets.

Map<String, Object> map1;
Map<String, Object> map2;

Set<Entry<String, Object>> diff12 = new HashSet<Entry<String, Object>>(map1.entrySet());
Set<Entry<String, Object>> diff21 = new HashSet<Entry<String, Object>>(map2.entrySet());
Set<Entry<String, Object>> result;

diff12.removeAll(map2.entrySet());
diff21.removeAll(map1.entrySet());
diff12.addAll(diff21);

Considering the awkward behavior you mentioned, let's take a closer look at the above code behavior. For example if we take the numerical example from the above given link:

Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("a", 1);
map1.put("b", 2);
map1.put("c", 3);
map1.put("d", 4);

Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("a", 1);    
map2.put("d", 4);
map2.put("e", 5);

After you calculate the difference as shown, the output:

System.out.println(Arrays.deepToString(diff12.toArray()));

gives:

[e=5, c=3, b=2]

which is the correct result. But, if we do it like this:

public class CustomInteger {
    public int val;

    public CustomInteger(int val) {
        this.val = val;
    }

    @Override
    public String toString() {
        return String.valueOf(val);
    }        
}   

map1.put("a", new CustomInteger(1));
map1.put("b", new CustomInteger(2));
map1.put("c", new CustomInteger(3));
map1.put("d", new CustomInteger(4));

map2.put("a", new CustomInteger(1));    
map2.put("d", new CustomInteger(4));
map2.put("e", new CustomInteger(5));

the same algorithm gives the following output:

[e=5, a=1, d=4, d=4, b=2, a=1, c=3]

which is not correct (and might be described as awkward :) )

In the first example the map is filled with int values wich are automatically boxed to Integer values.

The class Integer has its own implementation of equals and hashCode methods.

The class CustomInteger does not implement these methods so it inherits them from the omnipresent Object class.

The API doc for the removeAll method from the Set interface says the following:

Removes from this set all of its elements that are contained in the specified collection (optional operation). If the specified collection is also a set, this operation effectively modifies this set so that its value is the asymmetric set difference of the two sets.

In order to determine which elements are contained in both collections, the removeAll method uses the equals method of the collection element.

And that's the catch: Integer's equals method returns true if the two numeric values are the same, while Object's equals method will return true only if it is the same object, e.g. :

Integer a = 1; //autoboxing
Integer b = new Integer(1);
Integer c = 2;

a.equals(b); //  true
a.equals(c); //  false

CustomInteger d = new CustomInteger(1);
CustomInteger e = new CustomInteger(1);
CustomInteger f = new CustomInteger(2);

d.equals(e); //false
d.equals(f) // false

d.val == e.val //true
d.val == f.val //false

If it's still a bit fuzzy I strongly suggest reading the following tutorials:

Solution 4

    Set<Entry<String, Object>> diff = new HashSet<Entry<String, Object>>((map1.entrySet()));
    diff.addAll(map2.entrySet());//Union
    Set<Entry<String, Object>> tmp = new HashSet<Entry<String, Object>>((map1.entrySet()));
    tmp.retainAll(map2.entrySet());//Intersection
    diff.removeAll(tmp);//Diff

Solution 5

Building on Vlad's example to work with maps of different sizes

public static <K, V> Map<K, V> mapDiff(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right) {
        Map<K, V> difference = new HashMap<>();
        difference.putAll(left);
        difference.putAll(right);

        difference.entrySet().removeAll(left.size() <= right.size() ? left.entrySet() : right.entrySet());

        return difference;
    }
Share:
28,552
user710818
Author by

user710818

Updated on June 19, 2020

Comments

  • user710818
    user710818 almost 4 years

    I have two maps:

    Map<String, Object> map1;
    Map<String, Object> map2;
    

    I need to receive difference between these maps. Does exist may be apache utils how to receive this difference? For now seems need take entry set of each map and found diff1 = set1 - set2 and diff2 = set2- set1. After create summary map =diff1 + diff2 It looks very awkwardly. Does exist another way? Thanks.

  • user710818
    user710818 over 11 years
    Answer doesn't look correct. Map1 could contain map2 or map2 can contain map1, or be equal or difference in any direction can exist.
  • user710818
    user710818 over 11 years
    Thanks. I thought about guava, but for this need introduce new library in project, bette don't do this.
  • vitaly
    vitaly over 11 years
    @user710818 You wouldn't regret it - it is a great library
  • Koerr
    Koerr over 11 years
    @user710818 You should use it in your project
  • Amit Deshpande
    Amit Deshpande over 11 years
    It can be achieved in java if simple math is used. There is no need to introduce other library for that.
  • linski
    linski over 11 years
    the above answer has more elegantly solved the difference calculation!
  • Narasimha
    Narasimha over 5 years
    If the two maps have entries that differ in values but having same keys, then this library is quite handy code Map<K,MapDifference.ValueDifference<V>> entriesDiffering() Returns an unmodifiable map describing keys that appear in both maps, but with different values.
  • user
    user over 5 years
    stackoverflow.com/review/suggested-edits/21930992 this is a good java tool but keep in mind that it does not do a deep comparison only on first level.
  • William Reed
    William Reed about 5 years
    one thing to note is that this will lose any entries where only the values change and thus this is a diff on the keys