July 21, 2023September 5, 2023How to find out the differences between 2 objects in Java (2023) Difference between two java objectsIn 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.xmlThe classes and interfaces to be used for this post are as follows:org.apache.commons.lang3.builder.Difforg.apache.commons.lang3.builder.DiffResultorg.apache.commons.lang3.builder.DiffBuilderorg.apache.commons.lang3.builder.Diffable <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.6</version> </dependency> </dependencies>ScenarioWe 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 1For the Employee Object, we have the following data.Name = AnkitID = 1Designation = Assistant Vice PresidentAddress = MumbaiEmployee Object 2For the Employee Object, we have the following data.Name = AnkitID = 1Designation = Vice PresidentAddress = PuneJava CodesEmployee classIt is a Java Bean that represents an Employee Object. Two things worth highlighting in these codes.The class implements the Diffable interfaceThe class overrides the diff methodThis is where the magic happensimport 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 classThis 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] Like this:Like Loading...Related Java apache commons langDiffableDiffBuilderjava