How to receive difference of maps in java?

前端 未结 7 937
不知归路
不知归路 2021-02-05 09:36

I have two maps:

Map map1;
Map map2;

I need to receive difference between these maps. Does exist ma

7条回答
  •  日久生厌
    2021-02-05 09:54

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

    public static  Map mapDiff(Map left, Map right) {
            Map difference = new HashMap<>();
            difference.putAll(left);
            difference.putAll(right);
    
            difference.entrySet().removeAll(left.size() <= right.size() ? left.entrySet() : right.entrySet());
    
            return difference;
        }
    

提交回复
热议问题