Best Practice To Implement Equals & HashCode Methods In Java

I came across this very interesting finding the other day and I asked myself why didn’t I already know this. Well, let me explain – I had this assignment where I had to implement Equals & HashCode methods in each Class. Why I was doing this – I got to compare objects of these classes to verify if they are “equal” or not. Double quotes around equal mean that the comparison was for the values and not just the object references.

Reminder #1: equals() method in java.util.Object is as good as “==” i.e. it only compares the object references and not the values.

Reminder #2: If a class has overridden equals method, it got to override hashcode method [Do you need me to tell why? If yes, let me know in the comments section below. I hope not to disappoint you ;) ]

Okay, so that was the scenario and I wrote a pretty code for it as below. [Sample Code]

It looks good and of course it works well. UNTIL – any of the instance variable is null.

For one of the objects, name was set as null and when I was comparing, the hashCode method threw NullPointerException. Oh I hate NPEs but then this time they got me to learn something new…!!

So what should I do now, a null check for each instance variable’s value in hashCode method – duh, that will be too painful and messy.

I looked around and found out that Java 7 has this utility class java.util.Objects with two great methods among others as below:

  • equals(Object a, Object b)
  • Returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned. Otherwise, equality is determined by using the equals method of the first argument.

  • hash(Object… values)
  • Generates a hash code for a sequence of input values. The hash code is generated as if all the input values were placed into an array, and that array were hashed by calling Arrays.hashCode(Object[]).

I used this and my code became clean and it worked irrespective of the values of instance variables.

But I can’t use it – I am using Java 6. Duh…!! I looked around further and hurrah, my problem got solved – Guava i.e. Google Code For Java Libraries, provides a similar implementation of Objects class –

I included guava.jar and imported in my class and it worked like a charm.

So the bottom line is that using Java 7 utility class Objects or Guava utility class Objects, frees you from worrying about the actual values and even the knowledge of what each of it may mean.

Hope you liked the information shared here. If so, don’t forget it to share with your friends & colleagues :)

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">