Using an equals method is definitely the way to go. You don't have to write it yourself. Your IDE can generate it, or you can use a tool like Lombok to generate it at compile time. The latter has the advantage of automatically updating if fields are added or removed.

Answer from user8681 on Stack Overflow
🌐
Coderanch
coderanch.com › t › 378374 › java › Comparing-POJO-classes
Comparing POJO classes (Java in General forum at Coderanch)
November 17, 2005 - Then I iterate over it and compare the properties of the two POJO classes using "PropertyUtils.getProperty(...)". Here is code I wrote and its working: Thanks again for your help Steve. [ November 17, 2005: Message edited by: Wagner Danda ] SCJP, SCWCD · reply reply · Bookmark Topic Watch Topic · New Topic · Boost this thread! Similar Threads · Sorting arraylist on the basis of object · Difference between Comparable and Comparator · pass bean property values to javascript function ·
Top answer
1 of 4
2

Using an equals method is definitely the way to go. You don't have to write it yourself. Your IDE can generate it, or you can use a tool like Lombok to generate it at compile time. The latter has the advantage of automatically updating if fields are added or removed.

2 of 4
1

Since you are only checking the equality of the data, I wouldn't recommend overriding equals.
This is typically used to determine object equality, which is a slightly different context.

Just create a separate procedure.

An optimal approach, considering the database will have numerous values, would be to create a class to implement the procedure.
This will allow for the re-use of the variables during iterative invocations, thus reducing memory consumption.

In this example I created a class, UserEventUtil, with a single method, checkEquality.
It utilizes reflection to compare the opposing fields.

This should improve the scalability, and hence remove the requirement to explicitly type every field.

If you have any objects other than the basic scalar types here, you can just add them to the if-conditional checks, utilizing whatever data they may have to prove equality.

import java.lang.reflect.Field;

public class UserEventUtil {
    UserEvent userEventA, userEventB;
    Field[] fieldsA, fieldsB;
    Field fieldA, fieldB;
    Class<?> type;
    String stringA, stringB;
    int index;

    public UserEventUtil(UserEvent userEventA, UserEvent userEventB) {
        this.userEventA = userEventA;
        this.userEventB = userEventB;
    }

    boolean checkEquality() throws IllegalAccessException {
        fieldsA = userEventA.getClass().getDeclaredFields();
        fieldsB = userEventB.getClass().getDeclaredFields();
        for (index = 0; index < fieldsA.length; index++) {
            fieldA = fieldsA[index];
            fieldB = fieldsB[index];
            type = fieldA.getType();
            if (type == String.class) {
                stringA = (String) fieldA.get(userEventA);
                stringB = (String) fieldB.get(userEventB);
                if (stringA == null && stringB == null) continue;
                if (stringA == null || stringB == null) return false;
                else if (!stringA.equals(stringB)) return false;
            } else if (type == int.class) {
                if (fieldA.getInt(userEventA) != fieldB.getInt(userEventB))
                    return false;
            } else if (type == float.class) {
                if (fieldA.getFloat(userEventA) != fieldB.getFloat(userEventB))
                    return false;
            }
        }
        return true;
    }
}

And, to simulate the UserEvent class, I created a basic object with some bunk fields.

class UserEvent {
    String valueA, valueB, valueC;
    int valueD, valueE, valueF;
    float valueG, valueH, valueI;
}

Here is an example of comparing three separate values.

UserEvent userEventA = new UserEvent();
userEventA.valueA = "a";
userEventA.valueB = "b";
userEventA.valueC = "c";
userEventA.valueD = 1;
userEventA.valueE = 2;
userEventA.valueF = 3;
userEventA.valueG = 4.5f;
userEventA.valueH = 6.7f;
userEventA.valueI = 8.9f;

UserEvent userEventB = new UserEvent();
userEventB.valueA = "a";
userEventB.valueB = "b";
userEventB.valueC = "c";
userEventB.valueD = 1;
userEventB.valueE = 2;
userEventB.valueF = 3;
userEventB.valueG = 4.5f;
userEventB.valueH = 6.7f;
userEventB.valueI = 8.9f;

UserEvent userEventC = new UserEvent();
userEventC.valueA = null;
userEventC.valueB = "b";
userEventC.valueC = "c";
userEventC.valueD = 1;
userEventC.valueE = 2;
userEventC.valueF = 3;
userEventC.valueG = 4.5f;
userEventC.valueH = 6.7f;
userEventC.valueI = 8.9f;

UserEventUtil util;

util = new UserEventUtil(userEventA, userEventB);
System.out.println("util.checkEquality(), userEventA and userEventB = " + util.checkEquality());

util = new UserEventUtil(userEventB, userEventC);
System.out.println("util.checkEquality(), userEventB and userEventC = " + util.checkEquality());

Output

util.checkEquality(), userEventA and userEventB = true
util.checkEquality(), userEventB and userEventC = false
🌐
YouTube
youtube.com › retarget common
#24. |Rest Assured Framework | Find Difference Between 2 POJO Objects | Javers| Response Assertion - YouTube
#restassured #apitesting #tester #testing #equalshashcode #java #automationtestiingJavers library is a better way to find the difference between two POJO obj...
Published: March 18, 2023
Views: 1K
🌐
YouTube
youtube.com › retarget common
#22. |Rest Assured Framework| Response Assertion| Compare POJO Objects| Equals & HashCode Methods | - YouTube
#restassured #apitesting #tester #testing #equalshashcode #java #automationtestiingIn this video, I have explained how can we compare two POJO objects. GitHu...
Published: March 12, 2023
Views: 1K
🌐
Blogger
javabelazy.blogspot.com › 2017 › 07 › how-to-compare-two-pojo-model-objects.html
java source code: How to compare two pojo model objects in java based on a integer value
Source code /** * */ package com.cfed.oms; import java.util.Comparator; import java.util.Date; /** * @author IT * */ public class BranchModel implements Comparator<BranchModel> { private int branchId = 0; private String branchName = null; private int branchCode = 0; private String category = null; private String constituency = null; private String address = null; private String district = null; private Date openingDate = null; private Date closedDate = null; private Long lat; private Long lon; private String link = null; private String status = null; private int branchTypeId = 0; private Strin
🌐
Stack Overflow
stackoverflow.com › questions › 33943538 › comparing-two-pojos-in-java
equals - Comparing two POJOs in Java - Stack Overflow
November 26, 2015 - I have two objects which has multiple properties. Please note one of these objects is given to me via Db->JAXB route (from some XML schema) and hence I can't implement Comparator and Comparable on...
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 54848916 › efficient-comparison-between-two-pojos-to-determine-the-extent-of-how-similar-th
java - Efficient comparison between two POJOs to determine the extent of how similar they are - Stack Overflow
Meaning: there is reflection (see https://www.oracle.com/technetwork/articles/java/javareflection-1536171.html) for example that allows you to write code that inspects the fields of objects arbitrary classes. But that is extremely cumbersome and error prone, and of course: coming at high performance penalties at runtime. There are libraries build around that though, like the https://commons.apache.org/proper/commons-lang/javadocs/api-3.5/org/apache/commons/lang3/builder/EqualsBuilder.html that make things a bit easier to use.
🌐
Stack Overflow
stackoverflow.com › questions › 72187851 › find-difference-of-two-object-of-same-pojo-in-java
spring boot - Find difference of two object of same Pojo in Java - Stack Overflow
May 10, 2022 - public static Map difference(Organization current, Organization prev) throws IllegalArgumentException, IllegalAccessException{ Map changedProperties ...
🌐
Stack Overflow
stackoverflow.com › questions › 24164791 › compare-pojo-objects-in-java-for-equality-based-on-all-minus-few-fields
Compare pojo objects in java for equality based on all minus few fields - Stack Overflow
And the second is just what the computer has to do for the situation, there isn't anything to object to there. I'm left feeling like I don't understand what his problem is. ... Well if you don't mind spend little bit more memory and cpu time to compare this, good way to do it might be use one of json converters to convert your pojo model into json string and compare those two result string.
🌐
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.
🌐
Stack Overflow
stackoverflow.com › questions › 56683389 › compare-two-pojo-and-output-difference-to-another-pojo
java - Compare two pojo and output difference to another pojo - Stack Overflow
June 20, 2019 - In this case it's address. That new address should be put into the new Person-POJO class (with all other setters as NULL). ... I know there are few libraries with with we can compare beans with some comparators. But it will just compare, will not provide the changed part alone.
🌐
Javaprogramto
javaprogramto.com › 2020 › 08 › how-to-compare-two-objects-in-java-8.html
How To Compare Two Objects In Java 8 For Equality
August 9, 2020 - In this article, You've seen how to compare two objects in java using equals() method of Object class.
🌐
GitConnected
levelup.gitconnected.com › comparing-objects-with-java-reflection-api-ae613c4f1532
Comparing Objects with Java Reflection | Level Up Coding
February 13, 2024 - Comparison of objects is quite a common requirement across all Java and Spring Boot applications, and there are many ways to achieve this. Below are the most common approaches used by majority of the developers for comparing two objects in Java