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 OverflowIs there any easier way to compare all the attributes of 2 complex objects without overriding toequals method? The complex object which i am working on has nested objects and the level goes down pretty deep and refactoring of the same is not currently in my scope.
I need to write my test cases to compare 2 such objects and also reporting which all fields are not equal in case the junit test cases fails.
Comparing Java objects with different member variables - Software Engineering Stack Exchange
How to compare two Java objects?
what is the best way to compare two complex java object and generate event depending upon comparision - Stack Overflow
java - How to check custom equalness for complex objects? - Stack Overflow
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
You can use Bean Utils or directly use... Apache Commons EqualsBuilder
a similar solution
EDITED : Please have a look at this post. Upon googling I also found this BeanDiff api which may be useful to you.
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).
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, includingStudent, or - you may call some function/transformation that provides a unique value (e.g. a string) that allows to sort any
People.PeopleandStudentmay then just use a different transformation that will be passed to the comparator; Or - you only sort among homogeneous subtypes.
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!
Override the equals() and hashCode() method in your Policy class. Then you can check for equality like:
if(object1.equals(object2)) {
// do something
}
Implement Comparable and override the compareTo() method if you need to order the objects.
One solution: use Jackson to serialize your objects as JSON, then use this: it is a Java implementation of JSON Patch which also can generate differences between two JSONs as JSON Patches.
Which means you can know what has changed and where. And since this is JSON, you can send the result to your browser and have it handled by some JavaScript code easily. Unlike XML!