I want to write a java program which has to find the differences between the two objects which is of different instances. I have implemented it using equals() and comparator. But here I want to find differences and have to show that in logging format.
My program is below :
public class A implements Comparator<A>{
private int id1, id2;
/* setters and getters for id1 and id2 */
public boolean equals(Object arg0) {
if (this.getClass() != arg0.getClass()) {
return false;
}
if (((A) arg0).getId1() == this.id1 && ((A) arg0).getId2() == this.id2) {
return true;
}
return false;
}
public static void main(String args[]) {
A obj1 = new A();
obj1.id1 = 10;
obj1.id2 = 20;
A obj2 = new A();
obj2.id1 = 30;
obj2.id2 = 20;
/*
* equals comparison
*/
if (obj1.equals(obj2)) {
System.out.println("EQUALS");
} else {
System.out.println("NOT EQUALS");
}
}
Please can any body advise me how i can find the differences and show that in logging format.
Thanks.
To implement
Comparator<A>you need a methodpublic int compare(A o1, A o2). This is an example of such an implementation:Then you can use it like this:
It is probably more common to make another class implement the
Comparator<A>, instead of putting this inAitself.