Using Apache Commons Lang 3 for Efficient Object Comparison in Java
This article introduces Apache Commons Lang 3, explains its key utilities, and demonstrates how to integrate the library with Maven and employ DiffBuilder and ReflectionDiffBuilder to compare Java objects, generate detailed difference reports, and simplify debugging and testing processes.
Apache Commons Lang 3 Overview
Apache Commons Lang 3 is a widely used open‑source utility library for Java that simplifies everyday programming tasks such as string handling, object operations, and date‑time management. Its core features include StringUtils , ObjectUtils (providing null‑safe equals , hashCode , and toString ), DateUtils , RandomStringUtils , and WordUtils . The library reduces boilerplate code, improves readability, and helps avoid common errors.
Code Practice
To add Apache Commons Lang 3 to a Maven project, include the following dependency in pom.xml :
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>When the project is built, Maven automatically downloads the library and its transitive dependencies and adds them to the classpath.
DiffBuilder
DiffBuilder is a class in Apache Commons Lang 3 that compares two objects and produces a detailed diff report. The typical steps are:
Import the required classes.
Create the objects to be compared.
Use DiffBuilder to build the diff.
Inspect the resulting DiffResult for differences.
Example code:
package com.example;
import org.apache.commons.lang3.builder.DiffBuilder;
import org.apache.commons.lang3.builder.DiffResult;
// Define Person class
class Person {
private String name;
private int age;
// constructors, getters, setters omitted
}
public class Main {
public static void main(String[] args) {
// Create two Person objects
Person person1 = new Person("Alice", 30);
Person person2 = new Person("Bob", 35);
// Build diff report
DiffResult diffResult = new DiffBuilder(person1, person2)
.append("name", person1.getName(), person2.getName())
.append("age", person1.getAge(), person2.getAge())
.build();
// Output differences
System.out.println("Object differences:");
diffResult.getDiffs().forEach(diff ->
System.out.println(diff.getFieldName() + ": " + diff.getLeft() + " != " + diff.getRight())
);
}
}The example shows how DiffBuilder compares the name and age fields of two Person instances and prints the differences.
ReflectionDiffBuilder
ReflectionDiffBuilder uses reflection to automatically compare all fields of two objects, eliminating the need to list each field manually. This is useful for complex objects.
Import the necessary Apache Commons Lang classes.
Create the objects to compare.
Use ReflectionDiffBuilder to build the diff.
Inspect the generated DiffResult for differences.
Example code:
package com.example;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ReflectionDiffBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
// Define Person class
class Person {
private String name;
private int age;
// constructors, getters, setters omitted
@Override
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj);
}
@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
public class Main {
public static void main(String[] args) {
// Create two Person objects
Person person1 = new Person("Alice", 30);
Person person2 = new Person("Bob", 35);
// Build diff report using reflection
DiffResult diffResult = new ReflectionDiffBuilder(person1, person2).build();
// Output differences
System.out.println("Object differences:");
diffResult.getDiffs().forEach(diff ->
System.out.println(diff.getFieldName() + ": " + diff.getLeft() + " != " + diff.getRight())
);
}
}ReflectionDiffBuilder automatically compares all fields, including nested and private ones, and generates a comprehensive diff report.
Conclusion
Apache Commons Lang 3 provides a powerful set of tools for Java developers. For object comparison, DiffBuilder offers an intuitive way to pinpoint differences between objects, while ReflectionDiffBuilder handles complex objects automatically through reflection, greatly improving debugging efficiency and code quality.
FunTester Original Highlights 【Series】Performance Testing Starting from Java Chaos Engineering, Fault Testing, Web Frontend Server-side Functional Testing Performance Testing Topics Java, Groovy, Go White‑box, Tools, Crawlers, UI Automation Theory, Insights, Videos
FunTester
10k followers, 1k articles | completely useless
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.