Skip to content

Java equals() and hashcode() methods

When we start studying Java after learning that all objects extends from the Object class, it common to be introduced to the equals() and hashcode() methods and their contract. For someone that just started learning how to program it can be very confusing, specially regarding the contract between these two methods.

In this post, I will explain why is important to have these two methods, where they are actually used and their contract work under the hood.

A quick refresh

Before digging in, let’s recall how the equals and hashcode works.

  • equals(): this method compares if the value of two objects are equals. For example, if it is applied to compare strings like “Java”.equals(“Java”) the result will be true. It is different of how we use “==” since it compares the memory address. When creating our own objects it is important to implement a custom equals(), since the equality of our objects cannot be infered by the JDK.
  • hashcode(): return an integer associated with the object. Return the same value when called on the same object, provided data no data used by the equals() method is modified. When this method is called on two different objects, but they are equal (using the equals() method) it returns the same hash code value. However, if it is called on two unequal objects, the value will not necessarily be different.

In summary, these two methods should adhere to the following contract:

  • if object1.equals(object2)is true, then object.hashCode() == object2.hashCode() must return true
  • if object1.hashCode() == object2.hashCode() is true, then not necessarilly object1.equals(object2) will return true

Applying equals() and hashcode()

Suppose we want to create a Emplyee class as shown on the listing below:

Then this class will be used on any hash based collection (HashMap, HashSet, Hashtable), now you may be guessing “I think this Hash prefix has something related to the hashCode()”. Yes, you are absolutely right.

When we want to insert an object on a HashSet, what happens behind the scenes is that the object will be stored in a bucket and the position inside it will be determined by a hash function applied on the hashcode of the object. This is the first part of the process.

Now that the position on the bucket was found, there is the possibility of another object already exists on that position. Here the equals() comes to the rescue. When the previous situation occurs is called a hash collision. To solve this, inside each bucket will exist a binary tree. Then the equals() will be used to compared the newer object with the current placed objects and if exists an object with the same value, it will be replaced with the new.

In the image below, the internals of the hashmap is illustrated and we have a bucket and inside it there is a binary tree, sorted by the hashcode.

Conclusion

Equals() and hashCode() methods plays an important role when we are working with hash based collections. Also it is very important to follow their contract, otherwise this could cause an impact on the performance of our application, a bad implementation of these two methods can led to hash collisions and more processing.

Hope you enjoyed this post, and see you on the next content!

Published inUncategorized

Be First to Comment

Leave a Reply

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