Java: Clean way of avoiding NullPointerException in equals checks

前端 未结 8 1550
感情败类
感情败类 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:58

    There is no really clean way to do that; the best option is probably to have your IDE generate the code for you. Eclipse can do it via the Source -> Generate hashCode() and equals() context menu.

    0 讨论(0)
  • 2021-02-12 13:00

    You can use Objects.equal from Googles guava or the EqualsBuilder from apache commons

    0 讨论(0)
提交回复
热议问题