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?
There are a few ways to approach this.
You can roll your own - that has the highest likelihood of getting something subtle wrong.
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.
You can use EqualsBuilder; it avoids the aforementioned problems.
Best of all, at least in my experience, you can use lombok's EqualsAndHashCode annotation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With