EqualsBuilder vs own equals method

前端 未结 4 1905
小蘑菇
小蘑菇 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条回答
  •  旧时难觅i
    2021-01-13 05:32

    Using ANYTHING except for your own implementation in equals() is GUARANTEED to be "worse" if you can use ... say a strictly unique ID.

    If your ID is really unique you will most likely have the best, possible implementation with this, of course it needs to be polished quite a bit:

    @Override
    public boolean equals(Object other)
    {
       if(other instanceof MyClass)
       {
          MyClass obj = (MyClass)other;
          return obj.getID() == this.getID();
       }
       else
          return false;
    }
    

    Have a look at this, this and especially this

提交回复
热议问题