import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;

import java.util.Map;

public class MapUtil {

    /** * difference * maps. difference(Map, Map) compares two Maps to get all the differences. This method returns the MapDifference object */
    public static void u(Map<String,Object> map1, Map<String,Object> map2) {
        MapDifference<String, Object> difference = Maps.difference(map1, map2);
        // If there is a difference, return Boolean
        boolean areEqual = difference.areEqual();
        System.out.println("Compare two maps for differences :" + areEqual);
        // The intersection of two maps
        Map<String, Object> entriesInCommon = difference.entriesInCommon();
        System.out.println("Part of both maps (intersection) === :" + entriesInCommon);
        // The key is the same but the value is different. Returns the value of the Map type for MapDifference. ValueDifference, about to represent two different values
        Map<String, MapDifference.ValueDifference<Object>> entriesDiffering = difference.entriesDiffering();
        System.out.println("Same key but different value mapping item === :" + entriesDiffering);
        // The key exists only in the left side of the Map
        Map<String, Object> onlyOnLeft = difference.entriesOnlyOnLeft();
        System.out.println("The key only exists in the mapping item of the left Map :" + onlyOnLeft);
        // The key only exists in the right Map
        Map<String, Object> entriesOnlyOnRight = difference.entriesOnlyOnRight();
        System.out.println("The key only exists in the mapping item of the right-hand Map :"+ entriesOnlyOnRight); }}Copy the code