Map difference example

Google Guava Mapdifference represents the difference between two maps.

Setup

class Student {
    
    Integer id;
    String name;
    
    public Student(Integer id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    ...
}
Map<Integer, Student> geometryClass;
Map<Integer, Student> gymClass;

@Before
public void setUp () {

    List<Student> geoStudents = Lists.newArrayList(
            new Student(123, "Jack Johnson"),
            new Student(456, "Cindy Lewis"),
            new Student(789, "Terry Peters"),
            new Student(912, "Ethan Nicks"),
            new Student(234, "Sarah Perry")
    ); 
    
    geometryClass = Maps.uniqueIndex(geoStudents, new Function <Student, Integer> () {
          public Integer apply(Student student) {
            return student.id; 
    }});
    
    List<Student> gymStudents = Lists.newArrayList(
            new Student(123, "Jack Johnson"),
            new Student(478, "Patrick Ewig"),
            new Student(789, "Cindy Peters"),
            new Student(937, "Jon Lund"),
            new Student(234, "Sarah Perry")
    ); 

    gymClass = Maps.uniqueIndex(gymStudents, new Function <Student, Integer> () {
          public Integer apply(Student student) {
            return student.id; 
    }});

}

Entries in common

@Test
public void entries_in_common_or_intersection() {

    MapDifference<Integer, Student> mapDifference = Maps.difference(
            geometryClass, gymClass);

    Map<Integer, Student> commonStudents = mapDifference.entriesInCommon();

    logger.info(commonStudents);

    assertThat(commonStudents, hasKey(new Integer(234)));
    assertThat(commonStudents, hasKey(new Integer(123)));
}

Output

{
    234=Student{id=234, name=Sarah Perry}, 
    123=Student{id=123, name=Jack Johnson}
}

Entries that differ

@Test
public void entries_differing() {

    MapDifference<Integer, Student> mapDifference = Maps.difference(
            geometryClass, gymClass);

    System.out.println(mapDifference.entriesDiffering()); // with same keys

    Map<Integer, ValueDifference<Student>> sameKeyDifferentValue = mapDifference
            .entriesDiffering();

    assertThat(sameKeyDifferentValue.keySet(), hasItems(new Integer(789)));
}

Output

{
    789=(Student{id=789, name=Terry Peters}, Student{id=789, name=Cindy Peters})
}

Entries on the left

@Test
public void entries_only_on_left() {

    MapDifference<Integer, Student> mapDifference = Maps.difference(
            geometryClass, gymClass);

    Map<Integer, Student> studentsOnLeft = mapDifference
            .entriesOnlyOnLeft();

    logger.info(studentsOnLeft);

    assertThat(studentsOnLeft, hasKey(new Integer(456)));
    assertThat(studentsOnLeft, hasKey(new Integer(912)));
}

Output

{
    456=Student{id=456, name=Cindy Lewis}, 
    912=Student{id=912, name=Ethan Nicks}
}

Entries on the right

@Test
public void entries_only_on_right() {

    MapDifference<Integer, Student> mapDifference = Maps.difference(
            geometryClass, gymClass);

    Map<Integer, Student> studentsOnTheRight = mapDifference
            .entriesOnlyOnRight();

    logger.info(studentsOnTheRight);

    assertThat(studentsOnTheRight, hasKey(new Integer(478)));
    assertThat(studentsOnTheRight, hasKey(new Integer(937)));
}

Output

{
    478=Student{id=478, name=Patrick Ewig}, 
    937=Student{id=937, name=Jon Lund}
}