2012-07-06 79 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。差異包含更改列表(引用添加,屬性更改等)。