EqualsBuilder vs own equals method

前端 未结 4 1921
小蘑菇
小蘑菇 2021-01-13 05:25

I just came across a code using EqualsBuilder() in equals method. Is there any advantage of using it instead of writing (or generating from eclipse

4条回答
  •  清歌不尽
    2021-01-13 05:43

    There are a few ways to approach this.

    1. You can roll your own - that has the highest likelihood of getting something subtle wrong.

    2. You can have Eclipse generate your equals and hashCode methods for you - that leaves a lot of code in place, subject to inadvertent edits, and subject to failure to update when the class acquires a new field.

    3. You can use EqualsBuilder; it avoids the aforementioned problems.

    4. Best of all, at least in my experience, you can use lombok's EqualsAndHashCode annotation.

提交回复
热议问题