If you can ensure all the nested object tree implements Comparable you could use:

 public int compareTo(Object o) {
   return CompareToBuilder.reflectionCompare(this, o);
 }

http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/builder/CompareToBuilder.html

Answer from domgom on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › core java › comparing objects in java
Comparing Objects in Java | Baeldung
October 10, 2025 - In this tutorial, we’ll explore some of the features of the Java language that allow us to compare objects. We’ll also look at such features in external libraries. Let’s begin with the == and != operators, which can tell if two Java objects are the same or not, respectively.
Discussions

Comparing Java objects with different member variables - Software Engineering Stack Exchange
I have a base class "People" which two other classes inherit from: Employee and Student. The Student class includes a GPA (type double) and the Employee class does not. I have an ArrayLis... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
November 8, 2023
How to compare two Java objects?
By convention, each Java class has an equals method that is inherited from the base Object class. The actual implementation depends on the use case. In your example, you could check that the two objects have the same values for number, color and ball. More on reddit.com
🌐 r/javahelp
8
1
July 12, 2017
what is the best way to compare two complex java object and generate event depending upon comparision - Stack Overflow
I have two populated instance of this class. I want to compare those instance and depending upon difference I need to show them in UI marked in colors using some flag. What is the best way to compare these objects. Can we achieve it using XML because java code will be complex. More on stackoverflow.com
🌐 stackoverflow.com
May 23, 2017
java - How to check custom equalness for complex objects? - Stack Overflow
I have a complex object and want to check only specific properties if they are equal, and if yes the compared objects should be considered equal. I don't want to override the native equal() method... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Coderanch
coderanch.com › t › 441463 › java › Efficient-compare-complex-objects
Efficient way to compare complex objects? (Performance forum at Coderanch)
My objects might be built in different JVM so I'm worried that Hashcode may not be sufficient. ... It depends. Would you generally expect differences or matches? Are your lists private and only exposed by accessors? Personally, I think a hashCode would be sufficient provided you're not dealing with serialization or persistence and you expect a number of differences. In that case, you only have to do a deep equals comparison when there's a hashCode collision.
🌐
Runestone Academy
runestone.academy › ns › books › published › csjava › Unit3-If-Statements › topic-3-7-comparing-objects.html
3.7. Comparing Objects — CS Java
Often classes have their own equals method, which can be used to determine whether two objects of the class are equivalent. Two object references are considered aliases when they both reference the same object. Object reference values can be compared, using == and !=, to identify aliases.
🌐
LabEx
labex.io › tutorials › java-how-to-compare-java-objects-based-on-multiple-attributes-417392
How to compare Java objects based on multiple attributes | LabEx
The Comparator<T> interface defines a method compare(T o1, T o2) that compares two objects of the same type. Unlike Comparable, which is implemented by the class being compared, a Comparator is a separate class or lambda expression that can ...
🌐
Baeldung
baeldung.com › home › java › using the apache commons lang 3 for comparing objects in java
Using the Apache Commons Lang 3 for Comparing Objects in Java | Baeldung
January 8, 2024 - In this article, we demonstrated how to compare Java objects using the DiffBuilder and the ReflectionDiffBuilder from the Apache Commons Lang 3 library. Both classes are easy to use and offer a convenient way to compare objects, although each has advantages and disadvantages. We have seen through the examples in this article that DiffBuilder offers more customization and is more explicit. Still, it might result in increased complexity for more complex objects.
🌐
Newtum
blog.newtum.com › comparing-two-objects-in-java-using-equals-and-hashcode
Comparing Two Objects in Java: Using equals() and hashcode() - Newtum
October 11, 2023 - Implement the Comparable interface: The compareTo() method can be used to compare objects when the Comparable interface is implemented. Observe the agreement: Make sure the equals() method complies with the contract of equivalence, which mandates ...
Find elsewhere
🌐
Javatpoint
javatpoint.com › how-to-compare-two-objects-in-java
How to Compare Two Objects in Java - Javatpoint
How to Compare Two Objects in Java - How to Compare Two Objects in Java with java tutorial, features, history, variables, object, class, programs, operators, for-loop, oops concept, inheritance, array, string, map, math, methods, examples etc.
🌐
GitHub
gist.github.com › florianguigue › 0cf7d2dc2f561eb2ff59812f93cedcd0
Compare two complex Java objects · GitHub
Save florianguigue/0cf7d2dc2f561eb2ff59812f93cedcd0 to your computer and use it in GitHub Desktop. Download ZIP · Compare two complex Java objects · Raw · CompareObjects.java · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
🌐
Readthedocs
java-object-diff.readthedocs.io
java-object-diff Documentation
One of the simplest solutions that'll cross your mind is most certainly to use reflection to scan the object for fields or getters and use them to compare the values of the different object instances. In many cases this is a perfectly valid strategy and the way to go.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-compare-two-objects
Java Program to Compare Two Objects - GeeksforGeeks
July 23, 2025 - Though the values of dog1 and dog2 are the same, equals() method always checks the reference of the two objects i.e if both the objects passed refer to the same object or not and not their values.
Top answer
1 of 5
3

The “different member variables” is irrelevant. It’s an implementation detail. What you need is a set of rules which of two people comes first.

You could for example sort by family name, then given name, then date of birth, and if these are all three equal, take the name of the school, university or company (which will be different member variables) and compare them as strings. If that is equal, you might have student and employee ids, and the student ids might be unique, and the employee ids might be unique, but student and employee ids might be the same. So you could sort then students first ordered by id, followed by employees sorted by id, if you might sort by if first if student and employee ids are comparable.

(University or school and employer might be the same, because universities are also employers).

2 of 5
3

Comparing objects with different fields sounds like bad polymorphic design, whether it's Java or any other OOP language:

  • If your comparator needs to know the precise subtype of an object to do the comparison, you mess-up with the the open-closed principle, since for every new subclassing, you'd potentially need to modify the comparator to select the relevant fields.
  • If your comparator needs uses reflexion to find on its own the relevant fields to compare, you indirectly mess up with the principle of encapsulation, since you create a hidden requirement that information to be compared must be in some predetermined field.

If you want to sort People properly in a clean polymorphic design:

  • you need to rely either on a field, available for any kind of People, including Student, or
  • you may call some function/transformation that provides a unique value (e.g. a string) that allows to sort any People. People and Student may then just use a different transformation that will be passed to the comparator; Or
  • you only sort among homogeneous subtypes.
🌐
Reddit
reddit.com › r/javahelp › how to compare two java objects?
r/javahelp on Reddit: How to compare two Java objects?
July 12, 2017 -

Hi there, it's my first time learning Java and I'd love some assistance with comparing two Java objects. Here is a KenoBall class:

public class KenoBall {

    private static int number;
    private static String colour;
    private static String ball;

    public KenoBall(int number, String colour) {
        this.number = number;
        this.colour = colour;
        this.ball = number + colour;
        System.out.print(number + colour + "\n");
    } 

    public boolean matches(KenoBall other) {
        if (What goes in here?) {
            System.out.println("Tru");
            return true;
        } else {
            System.out.println("False");
            return false;
        }
    } 

 }

And in my main method:

    public static void main(String[] args) {
        KenoBall k1 = new KenoBall(1, "R");
        KenoBall k2 = new KenoBall(1, "R");
        KenoBall k3 = new KenoBall(4, "B");
    
        k1.matches(k2);
    }

If anyone can point me in the right direction I'd really appreciate it! Basically I'm trying to see if k1 is equal to k2 (and it should).

Thank you!

🌐
InfoWorld
infoworld.com › home › blogs › java challengers
Comparing Java objects with equals() and hashcode() | InfoWorld
May 16, 2024 - In the first comparison, equals() compares the current object instance with the object that was passed. If the two objects have the same values, equals() returns true. In the second comparison, equals() checks to see whether the passed object ...
🌐
HappyCoders.eu
happycoders.eu › java › comparator-comparable-compareto
compareTo, Comparable, Comparator - Comparing Objects in Java
June 12, 2025 - To sort objects, the program has ... GitHub repository. You compare Java primitives (int, long, double, etc.) using the operators <, <=, ==, =>, >....
🌐
Wikibooks
en.wikibooks.org › wiki › Java_Programming › Comparing_Objects
Comparing Objects - Wikibooks, open books for an open world
The == operator can be used to check if two object references point to the same object. ... To be able to compare two Java objects of the same class, the boolean equals(Object obj) method must be overridden and implemented by the class.
🌐
GeeksforGeeks
geeksforgeeks.org › java › compare-objects-by-multiple-fields-in-java
How to Compare Objects by Multiple Fields in Java? - GeeksforGeeks
July 23, 2025 - To compare objects by multiple fields in Java using a Custom Comparator, you can follow this approach, Suppose you have a class MyClass with fields field1, field2, and field3. You want to compare objects of this class first by field1, then by ...