Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java equals(): to reflect or not to reflect

This question is specifically related to overriding the equals() method for objects with a large number of fields. First off, let me say that this large object cannot be broken down into multiple components without violating OO principles, so telling me "no class should have more than x fields" won't help.

Moving on, the problem came to fruition when I forgot to check one of the fields for equality. Therefore, my equals method was incorrect. Then I thought to use reflection:

--code removed because it was too distracting--

The purpose of this post isn't necessarily to refactor the code (this isn't even the code I am using), but instead to get input on whether or not this is a good idea.

Pros:

  • If a new field is added, it is automatically included
  • The method is much more terse than 30 if statements

Cons:

  • If a new field is added, it is automatically included, sometimes this is undesirable
  • Performance: This has to be slower, I don't feel the need to break out a profiler
  • Whitelisting certain fields to ignore in the comparison is a little ugly

Any thoughts?

like image 464
oreoshake Avatar asked Sep 23 '08 23:09

oreoshake


People also ask

What is the difference between equals () and == in Java?

equals() method. The major difference between the == operator and . equals() method is that one is an operator, and the other is the method. Both these == operators and equals() are used to compare objects to mark equality.

What is the importance of Hashcode () and equals () methods?

An object hash code value can change in multiple executions of the same application. If two objects are equal according to equals() method, then their hash code must be same. If two objects are unequal according to equals() method, their hash code are not required to be different.

Why do we override equals method in Java?

We can override the equals method in our class to check whether two objects have same data or not.

Why Java string equals vs ==?

The main difference between the . equals() method and == operator is that one is a method, and the other is the operator. We can use == operators for reference comparison (address comparison) and . equals() method for content comparison.


2 Answers

If you did want to whitelist for performance reasons, consider using an annotation to indicate which fields to compare. Also, this implementation won't work if your fields don't have good implementations for equals().

P.S. If you go this route for equals(), don't forget to do something similar for hashCode().

P.P.S. I trust you already considered HashCodeBuilder and EqualsBuilder.

like image 175
Hank Gay Avatar answered Sep 20 '22 07:09

Hank Gay


Use Eclipse, FFS!

Delete the hashCode and equals methods you have.

Right click on the file.

Select Source->Generate hashcode and equals...

Done! No more worries about reflection.

Repeat for each field added, you just use the outline view to delete your two methods, and then let Eclipse autogenerate them.

like image 27
MetroidFan2002 Avatar answered Sep 19 '22 07:09

MetroidFan2002