2012-07-06 77 views
3

我想编写一个java程序,它必须找到不同实例的两个对象之间的区别。我已经使用equals()和comparator来实现它。但在这里我想找到差异,并且必须以日志格式显示。java中2个对象之间的区别

我的程序低于:

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"); 
    } 

} 

请任何机构可以告诉我如何我能找到的差异,并表明,在日志记录格式。

谢谢。

+1

对于每个属性,比较。对于每个差异,记录。生成值的列表(或列表)可能更容易,然后对每个元素使用通用比较器,或者使用List >(或Map 或其他)还包括属性信息等。 – 2012-07-06 06:02:07

+0

是否要比较每个对象的所有属性或比较对象是否是不同的实例?如果要比较的每个属性试试这个[示例](http://stackoverflow.com/questions/10927781/is-there-a-way-to-replace-all-occurrences-of-a-string-in-一个列表/ 10928498#10928498)如果你想做第二个尝试使用'hashCode()'。 – Crazenezz 2012-07-06 06:15:09

回答

2

要实现Comparator<A>你需要一个方法public int compare(A o1, A o2)。这是这样一个实现的例子:

@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(); 
    } 
} 

然后你可以使用它像这样:

if (obj1.compare(obj1, obj2) == 0) { 
     System.out.println("EQUALS"); 
    } else { 
     System.out.println("NOT EQUALS"); 
    } 

这可能是更常见再拍类实现Comparator<A>,而不是把这个在A本身。

+0

我认为你指的是'compareTo'方法。 – npinti 2012-07-06 06:49:40

+0

不,这将在'Comparable '接口 – Keppil 2012-07-06 06:54:51

+0

确实你是对的。我从来没有见过这种方法。感谢:) – npinti 2012-07-06 06:57:21

0

您可以更改您的equals方法,并比较每个属性。对于你做的每一个比较,你记录你的结果。像这样的东西应该工作:

@Override 
public boolean equals(Object arg0) { 
    if (this.getClass() != arg0.getClass()) { 
     return false; 
    } 

    boolean same = true; 
    //LOG: Comparing ((A) arg0).getId1() with this.id1 
    if (((A) arg0).getId1() == this.id1) { 
     //LOG: Property Id1 is the same for both: Value of Id1 = this.id1   
    } 
    else { 
     //LOG: Property Id1 is not the same. Source Id1 = ((A) arg0).getId1() , target Id1 = this.id1 
     same = false; 
    } 

    if (((A) arg0).getId2() == this.id2) { 
     //LOG: Property Id2 is the same for both: Value of Id2 = this.id2   
    } 

    else { 
     //LOG: Property Id2 is not the same. Source Id2 = (((A) arg0).getId2(), target Id2 = this.id2 
     same = false; 
    } 

    // 
    return same; 
} 

编辑:我完全不被理解你的意思,当你说自己比较。如果你想比较自己可以做些像这样:

public boolean areTheSame(Object arg1, Object arg2) 
{ 
    if (arg1.getClass() != arg0.getClass()) { 
     return false; 
    } 

    boolean same = true; 
    //LOG: Comparing ((A) arg0).getId1() with arg1.id1 
    if (((A) arg0).getId1() == arg1.id1) { 
     //LOG: Property Id1 is the same for both: Value of Id1 = arg1.id1   
    } 
    else { 
     //LOG: Property Id1 is not the same. Source Id1 = ((A) arg0).getId1() , target Id1 = arg1.id1 
     same = false; 
    } 

    if (((A) arg0).getId2() == arg1.id2) { 
     //LOG: Property Id2 is the same for both: Value of Id2 = this.id2   
    } 

    else { 
     //LOG: Property Id2 is not the same. Source Id2 = (((A) arg0).getId2(), target Id2 = arg1.id2 
     same = false; 
    } 

    // 
    return same; 
} 

你可以在你的主类抛出此,并调用它,而不是这些行:

if (obj1.equals(obj2)) { 
    System.out.println("EQUALS"); 
} else { 
    System.out.println("NOT EQUALS"); 
} 

所以你只是做:

if (areTheSame(obj1, obj2)) 
{ 
    System.out.println("They are equal"); 
} 
else 
{ 
    System.out.println("They are not equal"); 
} 
+0

感谢您的回复。但是如果我想用我自己的比较器来检查差异,我该如何做到这一点。请告诉我这个。 – Dhruthi 2012-07-06 06:22:00

+0

@Dhruthi:你的类中的'equals'方法是不是你自己的比较器的重载实现? – npinti 2012-07-06 06:26:41

+0

嗨npinti,我有一个要求,用自己的比较(有取两个任意对象,并找到差异,并显示结果记录)找到2个对象是同一个实例中的差异。我用equlas()完成了它。但我不记得我可以如何与我自己的比较级。请告诉我这个。 – Dhruthi 2012-07-06 06:36:47

1

您可以使用java Reflection来比较两个相同类型的bean,这些bean具有getter的所有比较属性。

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); 
      } 
     } 
     } 

用法:

如果我比较Student类,其具有两个属性nameage

BeanComparator.compareBeans(new Student("Amita", 21), new Student("Amit", 23) , props); 

输出的两个bean:

Property = age Value of been1 =21 : Value of bean2 =23 
Property = name Value of been1 =Amita : Value of bean2 =Amit 
+0

它也可以使用子对象吗?例如,如果学生有地址列表,那么它是否也可以比较它们? – Anand 2013-12-02 09:46:22

0

你可以使用阿帕奇百科全书EqualsBuilder来生成equals()方法

给出一个类:

public class Person { 
    private long id; 
    private String firstName; 
    private String lastName; 
} 

你的equals方法看起来像

public boolean equals(Object object) { 
    if (!(object instanceof Person)) { 
     return false; 
    } 
    Person rhs = (Person) object; 
    return new EqualsBuilder() 
     .appendSuper(super.equals(object)) 
     .append(firstName, rhs.firstName) 
     .append(lastName, rhs.lastName) 
     .isEquals(); 
} 

或者,如果你想这样做使用反射

public boolean equals(Object o) { 
    return EqualsBuilder.reflectionEquals(this, o); 
} 
0

您可以使用开源工具javers - https://github.com/javers/javers。在Javers中,您可以找到方法比较(Object one,Object another)和return Diff。差异包含更改列表(引用添加,属性更改等)。