Java: Clean way of avoiding NullPointerException in equals checks

前端 未结 8 1544
感情败类
感情败类 2021-02-12 12:27

I have an address object that I want to create an equals method for. I could have made this quite simple by doing something like the following (shortened a bit):



        
8条回答
  •  我在风中等你
    2021-02-12 12:56

    Apache Commons Lang provides the EqualsBuilder helper class for equality comparissons. There is also one for hash codes.

    return new EqualsBuilder()
    .append(this.getStreet(), other.getStreet())
    .append(this.getStreetNumber(), other.getStreetNumber()
    .append(this.getStreetLetter(), other.getStreetLetter())
    .append(this.getTown(), other.getTown())).isEquals();
    

提交回复
热议问题