Might be a little late, but I was in the same situation like you and ended up creating my own library for exactly your use-case. Since I was forced to come up with a solution myself, I decided to release it on Github, to spare others the hard work. You can find it here: https://github.com/SQiShER/java-object-diff
--- Edit ---
Here is a little usage example based on the OPs code:
SomeClass a = new SomeClass();
SomeClass b = new SomeClass();
a.setProp1("A");
a.setProp2("X");
b.setProp1("B");
b.setProp2("X");
DiffNode diff = ObjectDifferBuilder.buildDefault().compare(a, b);
assert diff.hasChanges();
assert diff.childCount() == 1;
assert diff.getChild('prop1').getState() == DiffNode.State.CHANGED;
Answer from SQiShER on Stack OverflowMight be a little late, but I was in the same situation like you and ended up creating my own library for exactly your use-case. Since I was forced to come up with a solution myself, I decided to release it on Github, to spare others the hard work. You can find it here: https://github.com/SQiShER/java-object-diff
--- Edit ---
Here is a little usage example based on the OPs code:
SomeClass a = new SomeClass();
SomeClass b = new SomeClass();
a.setProp1("A");
a.setProp2("X");
b.setProp1("B");
b.setProp2("X");
DiffNode diff = ObjectDifferBuilder.buildDefault().compare(a, b);
assert diff.hasChanges();
assert diff.childCount() == 1;
assert diff.getChild('prop1').getState() == DiffNode.State.CHANGED;
http://javers.org is library that does exacly what you need: has methods like compare(Object leftGraph, Object rightGraph) returning the Diff object. Diff contains a list of changes (ReferenceChange, ValueChange, PropertyChange) e.g.
given:
DummyUser user = dummyUser("id").withSex(FEMALE).build();
DummyUser user2 = dummyUser("id").withSex(MALE).build();
Javers javers = JaversTestBuilder.newInstance()
when:
Diff diff = javers.compare(user, user2)
then:
diff.changes.size() == 1
ValueChange change = diff.changes[0]
change.leftValue == FEMALE
change.rightValue == MALE
It can handle cycles in graphs.
In addition you can get Snapshot of any graph object. Javers has JSON serializers and deserializers to snapshot, and changes so you can easily save them in database. With this library you can easily implement a module for auditing.
Differences between 2 objects in java - Stack Overflow
How to compare two Java objects?
How does Java "==" operator check equality between objects? Trying to understand how Object.equals() works
Fastest way to compare property values in two variables?
To implement Comparator<A> you need a method public int compare(A o1, A o2). This is an example of such an implementation:
@Override
public int compare(A o1, A o2) {
if (o1 == o2) {
return 0;
} else if (o1 == null) {
return -1;
} else if (o2 == null) {
return 1;
}
if (o1.getId1() != o2.getId1()) {
return o1.getId1() - o2.getId1();
} else {
return o1.getId2() - o2.getId2();
}
}
Then you can use it like this:
if (obj1.compare(obj1, obj2) == 0) {
System.out.println("EQUALS");
} else {
System.out.println("NOT EQUALS");
}
It is probably more common to make another class implement the Comparator<A>, instead of putting this in A itself.
You can use java Reflection to compare two same type beans those have getter for all comparing properties.
public static void compareBeans(Object bean1, Object bean2, String... propertyNames)
throws IntrospectionException,
IllegalAccessException, InvocationTargetException {
Set<String> names = new HashSet<String>(Arrays
.asList(propertyNames));
BeanInfo beanInfo = Introspector.getBeanInfo(bean1
.getClass());
for (PropertyDescriptor prop : beanInfo
.getPropertyDescriptors()) {
if (names.remove(prop.getName())) {
Method getter = prop.getReadMethod();
Object value1 = getter.invoke(bean1);
Object value2 = getter.invoke(bean2);
if (value1 == value2
|| (value1 != null && value1.equals(value2))) {
continue;
}
System.out.println("Property = "+prop.getName() +" Value of been1 ="+value1 +" : Value of bean2 ="+value2);
}
}
}
Usage:
If i compare two beans of Student class which has two properties name and age like
BeanComparator.compareBeans(new Student("Amita", 21), new Student("Amit", 23) , props);
Output:
Property = age Value of been1 =21 : Value of bean2 =23
Property = name Value of been1 =Amita : Value of bean2 =Amit
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!