DEV Community

Rajesh Mishra
Rajesh Mishra

Posted on

How to find out the differences between 2 objects in Java (2023)

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

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

Read complete tutorial

Top comments (0)