2012-02-11 57 views
0

我創建了一個Address類象下面是相同的,那麼我想檢查兩個Address es.If的平等所有字段是相同的,這兩個Address ES被認爲是相同的。地址平等失敗,即使所有的字段都

因此,我實施了hashCodeequals方法。

public class Address{ 

    public String addressLine1; 

    public String addressLine2; 

    public String city;  

    public String state; 

    public String pincode; 

    public String phoneNumber;  

    public String country; 

    public Address() { 

    } 

    public Address(String addressLine1, String addressLine2, String city, 
      String state, String pincode, String phoneNumber, String country) {   
     this.addressLine1 = addressLine1; 
     this.addressLine2 = addressLine2; 
     this.city = city; 
     this.state = state; 
     this.pincode = pincode; 
     this.phoneNumber = phoneNumber; 
     this.country = country; 
    } 

    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = super.hashCode(); 
     result = prime * result 
       + ((addressLine1 == null) ? 0 : addressLine1.hashCode()); 
     result = prime * result 
       + ((addressLine2 == null) ? 0 : addressLine2.hashCode()); 
     result = prime * result + ((city == null) ? 0 : city.hashCode()); 
     result = prime * result + ((country == null) ? 0 : country.hashCode()); 
     result = prime * result 
       + ((phoneNumber == null) ? 0 : phoneNumber.hashCode()); 
     result = prime * result + ((pincode == null) ? 0 : pincode.hashCode()); 
     result = prime * result + ((state == null) ? 0 : state.hashCode()); 
     return result; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     System.out.println("equals("+obj); 
     if (this == obj) 
      return true; 
     if (!super.equals(obj)) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     Address other = (Address) obj; 
     if (addressLine1 == null) { 
      if (other.addressLine1 != null) 
       return false; 
     } else if (!addressLine1.equals(other.addressLine1)) 
      return false; 
     if (addressLine2 == null) { 
      if (other.addressLine2 != null) 
       return false; 
     } else if (!addressLine2.equals(other.addressLine2)) 
      return false; 
     if (city == null) { 
      if (other.city != null) 
       return false; 
     } else if (!city.equals(other.city)) 
      return false; 
     if (country == null) { 
      if (other.country != null) 
       return false; 
     } else if (!country.equals(other.country)) 
      return false; 
     if (phoneNumber == null) { 
      if (other.phoneNumber != null) 
       return false; 
     } else if (!phoneNumber.equals(other.phoneNumber)) 
      return false; 
     if (pincode == null) { 
      if (other.pincode != null) 
       return false; 
     } else if (!pincode.equals(other.pincode)) 
      return false; 
     if (state == null) { 
      if (other.state != null) 
       return false; 
     } else if (!state.equals(other.state)) 
      return false; 
     return true; 
    } 

    public String toString() { 
     return this.addressLine1+","+this.addressLine2+","+this.city+","+this.state+","+this.pincode+","+this.country+","+this.phoneNumber; 
    } 

} 

在測試用例這個類,我試圖創建兩個相同的地址,並且調用然而assertEquals..This失敗..

類AddressTests延伸單元測試{

@Test 
public void testAddressEquality() { 
    Address address1 = new Address(); 
    address1.addressLine1 = "#1000,South Avenue"; 
    address1.state = "New York"; 
    address1.country = "U.S"; 
    System.out.println("address1="+address1); 

    Address address2 = new Address(); 
    address2.addressLine1 = "#1000,South Avenue"; 
    address2.state = "New York"; 
    address2.country = "U.S"; 
    System.out.println("address2="+address2); 

    assertEquals(address1,address2); 

} 

}

assertEquals failed

Failure, expected: models.Address<#1000,South Avenue,null,null,New York,null,U.S,null> but was: models.Address<#1000,South Avenue,null,null,New York,null,U.S,null> 

有人可以幫我理解爲什麼這會失敗嗎?

+0

您是否已經在調試器中逐步完成測試以確定哪條線路出現故障? – 2012-02-11 14:33:16

+0

謝謝你的建議..我發現'if(!super.equals(obj)) \t \t \t return false;'是返回false的行。這是錯誤的嗎? – 2012-02-11 14:37:54

回答

3

這是一個問題:

if (!super.equals(obj)) 
     return false; 

Object.equals檢查引用是相同的(相同的測試爲您this == obj測試)。你根本不需要那個檢查 - 你已經測試過參考平等,如果對象引用不相同,你不想保釋。

形式the docs

的等於Object類的方法實現對象上差別可能性最大的相等關係;也就是說,對於任何非空引用值x和y,當且僅當x和y引用同一對象(x == y的值爲true)時,此方法返回true。

一個實現(OpenJDK):

public boolean equals(Object obj) { 
    return (this == obj); 
} 
0

我想你也應該修改散列函數。爲什麼不把其他哈希值的總和?如果您擁有該方法,那麼如果大多數字段不爲空,則結果超過20億(Integer.MAX_VALUE),因此您將環繞整數值。

0

你應該永遠不會創建hashCode/equals手動 ...讓你的IDE選擇爲你做。

在IntelliJ IDEA中簡化了ALT + Insert,生成hashCode/equals。 Eclipse會有點類似。