Tags

,

Google Guava is an open source library which provides lots of useful utility to Java programmer,  one of them is easy way to find the difference between two maps.
MapDifference of two Maps

//<![CDATA[
if(showAdsense){
document.write("(adsbygoogle = window.adsbygoogle || []).push({});”)
}
//]]>

public void getMapDiff() {
    Map<Integer,Integer> leftMap = ImmutableMap.of(1, 1, 2, 2, 4, 40);
    Map<Integer,Integer> rightMap = ImmutableMap.<Integer,Integer> builder()
        .put(2, 2).put(3, 3).put(4, 41).build();
    MapDifference<Integer,Integer> diff = Maps.difference(leftMap, rightMap);
    
    // output: entriesInCommon:{2=2}
    System.out.println("entriesInCommon:" + diff.entriesInCommon());
    // output: entriesOnlyOnLeft:{1=1}
    System.out.println("entriesOnlyOnLeft:" + diff.entriesOnlyOnLeft());
    // output: entriesOnlyOnRight:{3=3}
    System.out.println("entriesOnlyOnRight:" + diff.entriesOnlyOnRight());
    
    Map<Integer,ValueDifference<Integer>> entriesDiffering = diff
        .entriesDiffering();
    // output: entriesDiffering: {4=(40, 41)}
    System.out.println("entriesDiffering: " + entriesDiffering);
  }

via Blogger http://ift.tt/1ok0zDr

Advertisements