Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EqualsBuilder vs own equals method

Tags:

java

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) our own logic? A simple example would be more helful.

Edit : If it doesn't have any benefits than having less code in the class, isn't there is overhead of reflection?

like image 848
Vallabh Patade Avatar asked Oct 20 '22 04:10

Vallabh Patade


1 Answers

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.

like image 53
Carl Manaster Avatar answered Oct 22 '22 16:10

Carl Manaster