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 OverflowUsing 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.
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
Override the toString() method in your pojo class like below
@Override
public String toString() {
return "brand: " + this.brand + ",color: " + this.color;
}
car1.toString().equals(car2.toString()); //It will return true if both objects has same values
In case you have large nos of parameter i will suggest you to go with bellow code
public static boolean comparePOJO(Object obj1, Object obj2) {
return new Gson().toJson(obj1).equals(new Gson().toJson(obj2));
}
comparePOJO(car1,car2); //It will return true
This type of reflection is built into Hamcrest as SamePropertyValuesAs, which compares bean-named properties (getFoo, isBar) instead of the fields that likely power them. Core Hamcrest support is built into JUnit, so you'd just need to add the Hamcrest library that includes the SamePropertyValuesAs matcher.
assertThat(car1, samePropertyValuesAs(car2));
You need to provide your own implementation of equals() in MyClass.
@Override
public boolean equals(Object other) {
if (!(other instanceof MyClass)) {
return false;
}
MyClass that = (MyClass) other;
// Custom equality check here.
return this.field1.equals(that.field1)
&& this.field2.equals(that.field2);
}
You should also override hashCode() if there's any chance of your objects being used in a hash table. A reasonable implementation would be to combine the hash codes of the object's fields with something like:
@Override
public int hashCode() {
int hashCode = 1;
hashCode = hashCode * 37 + this.field1.hashCode();
hashCode = hashCode * 37 + this.field2.hashCode();
return hashCode;
}
See this question for more details on implementing a hash function.
You need to Override equals and hashCode.
equals will compare the objects for equality according to the properties you need and hashCode is mandatory in order for your objects to be used correctly in Collections and Maps