July 21, 2023August 19, 2024 How to find out the differences between 2 objects in Java (2023) [web_stories title=”true” excerpt=”false” author=”false” date=”false” archive_link=”true” archive_link_label=”” circle_size=”150″ sharp_corners=”false” image_alignment=”left” number_of_columns=”1″ number_of_stories=”5″ order=”DESC” orderby=”post_title” view=”circles” /] In this post, we will showcase a step-by-step guide on how to detect and present distinctions between two java objects by leveraging a set of classes and interfaces provided by Apache Commons Lang. Classes, Interfaces, and pom.xml The classes and interfaces to be used for this post are as follows: org.apache.commons.lang3.builder.Diff org.apache.commons.lang3.builder.DiffResult org.apache.commons.lang3.builder.DiffBuilder org.apache.commons.lang3.builder.Diffable <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.6</version> </dependency> </dependencies> Scenario We have two Employee Object for our use case, and we need to list the differences between them. The list must show the affected field and the old-new (from Employee 1 to Employee 2 values. Employee Object 1 For the Employee Object, we have the following data. Name = Ankit ID = 1 Designation = Assistant Vice President Address = Mumbai Employee Object 2 For the Employee Object, we have the following data. Name = Ankit ID = 1 Designation = Vice President Address = Pune Java Codes Employee class It is a Java Bean that represents an Employee Object. Two things worth highlighting in these codes. The class implements the Diffable interface The class overrides the diff method This is where the magic happens import org.apache.commons.lang3.builder.DiffBuilder; import org.apache.commons.lang3.builder.DiffResult; import org.apache.commons.lang3.builder.Diffable; import org.apache.commons.lang3.builder.ToStringStyle; public class Employee implements Diffable<Employee>{ private int id; private String name; private String designation; private String address; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDesignation() { return designation; } public void setDesignation(String designation) { this.designation = designation; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public DiffResult diff(Employee obj) { return new DiffBuilder(this, obj, ToStringStyle.SHORT_PREFIX_STYLE) .append("id", this.id, obj.id) .append("name", this.name, obj.name) .append("designation", this.designation, obj.designation) .append("address", this.address, obj.address) .build(); } } DiffableDemo class This is the main class – the entry point. import org.apache.commons.lang3.builder.Diff; import org.apache.commons.lang3.builder.DiffResult; public class DiffableDemo { public static void main(String[] args) { Employee avpEmp = new Employee(); avpEmp.setId(1); avpEmp.setName("Ankit"); avpEmp.setAddress("Mumbai"); avpEmp.setDesignation("Assistance Vice President"); Employee vpEmp = new Employee(); vpEmp.setId(1); vpEmp.setName("Ankit"); vpEmp.setAddress("Pune"); vpEmp.setDesignation("Vice President"); DiffResult diff = avpEmp.diff(vpEmp); for(Diff<?> d: diff.getDiffs()) { System.out.println(d.getFieldName() + "= FROM[" + d.getLeft() + "] TO [" + d.getRight() + "]"); } System.out.println(); } } This outputs: title= FROM[Mumbai] TO [Pune] content= FROM[Assistance Vice President] TO [Vice President] Java apache commons langDiffableDiffBuilderjava