开发者

creating hashCode and equals for Address

I need to implement equals() and hashCode() for an A开发者_开发百科ddress class.

I believe,the non null fields are taken to determine hashCode() and equals().In my application,Any of the fields except addressLine1 and country can be null.If that is the case,what happens if two different addresses have the same addressline1 and country?

Address1:(in state of NH which is omitted by user)
addressline1:111,maple avenue
country: US

Address2:
addressline1:111,maple avenue
state: Illinois
country: US

In such cases if I build a hashCode based only on non null fields ,it will give same for both addresses above.

Is this the right way to create hashCode?

int hash = addressline.hashCode();
if(addressLine2!=null){
   hash += addressLine2.hashCode();
}
and so on...


Typically you would check whether one is null and the other is not in your equals method. For hashcode, you would just use 0 as the null hashcode. Example:

public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((addressLine1 == null) ? 0 : addressLine1.hashCode());
    result = prime * result + ((state == null) ? 0 : state.hashCode());
    result = prime * result + ((country == null) ? 0 : country.hashCode());
    return result;
}

If you use an IDE, it will usually generate these for you. In eclipse, choose Source, Generate equals and hashcode and it will let you select the fields you want to be a part of your equals and hashcode methods. For the equals method and your fields, this is what eclipse creates:

public boolean equals(Object obj) {
    if (this == obj) return true;
    if (obj == null) return false;
    if (getClass() != obj.getClass()) return false;
    YourClass other = (YourClass) obj;
    if (addressLine1 == null) {
        if (other.addressLine1 != null) return false;
    } else if (!addressLine1.equals(other.addressLine1)) return false;
    if (country == null) {
        if (other.country != null) return false;
    } else if (!country.equals(other.country)) return false;
    if (state == null) {
        if (other.state != null) return false;
    } else if (!state.equals(other.state)) return false;
    return true;
}

I would use that as a starting point and make any changes you think are neccessary from there.


Even fields that are null should be compared for equality. Use code like the following to compare two fields of nonprimitive types, like String:

this.addressline==null ? other.addressline==null : this.addressline.equals(other.addressline)

For hash codes, use the same fields you used in equals, but you can treat null values as having a hash code of 0 (or any other hash code value).

Here's the canonical question:

What issues should be considered when overriding equals and hashCode in Java?

And here are discussions of libraries that help you implement these methods properly:

Apache Commons equals/hashCode builder (also discusses Guava)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜