Why do i need to override the equals and hashcode methods in Java?

Rayen Cherni
6 min readOct 15, 2022

--

Overview

In this article, we’ll introduce two methods that closely belong together: equals() and hashcode(). We’ll focus on their relationship with each other, how to correctly override them, and why we should override both or neither.

Why we override equals() method?

In Java we can not overload how operators like ==, +=, -+ behave. They are behaving a certain way. So let’s focus on the operator ==for our case here.

How operator == works?

It checks if 2 references that we compare point to the same instance in memory. Operator == will resolve to true only if those 2 references represent the same instance in memory.

So now let’s consider the following example:

public class Person {      private Integer age;
private String name;

..getters, setters, constructors
}

So let’s say that in your program you have built 2 Person objects on different places and you wish to compare them.

Person person1 = new Person("Mike", 34);
Person person2 = new Person("Mike", 34);
System.out.println( person1 == person2 ); --> will print false!

Those 2 objects from business perspective look the same right? For JVM they are not the same. Since they are both created with new keyword those instances are located in different segments in memory. Therefore the operator == will return false.

But if we can not override the ==operator how can we say to JVM that we want those 2 objects to be treated as same. There comes the .equals() method in play.

You can override equals() to check if some objects have same values for specific fields to be considered equal.

You can select which fields you want to be compared. If we say that 2 Person objects will be the same if and only if they have the same age and same name, then the IDE will create something like the following for automatic generation of equals() .

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age &&
name.equals(person.name);
}

Let’s go back to our previous example

Person person1 = new Person("Mike", 34);
Person person2 = new Person("Mike", 34);
System.out.println ( person1 == person2 ); --> will print false!
System.out.println ( person1.equals(person2) ); --> will print true!

So we can not overload == operator to compare objects the way we want but Java gave us another way, the equals() method, which we can override as we want.

Keep in mind however, if we don’t provide our custom version of .equals() (a.k.a override) in our class then the predefined .equals() from Object class and == operator will behave exactly the same.

Default equals() method which is inherited from Object will check whether both compared instances are the same in memory!

Why we override hashCode() method?

Some Data Structures in java like HashSet, HashMap store their elements based on a hash function which is applied on those elements. The hashing function is the hashCode() .

If we have a choice of overriding .equals() method then we must also have a choice of overriding hashCode() method. There is a reason for that.

Default implementation of hashCode() which is inherited from Object considers all objects in memory unique!

Let’s get back to those hash data structures. There is a rule for those data structures.

HashSet can not contain duplicate values and HashMap can not contain duplicate keys.

HashSet is implemented with a HashMap behind the scenes where each value of a HashSet is stored as a key in a HashMap.

So we have to understand how a HashMap works.

In a simple way a HashMap is a native array that has some buckets (bucket is a fancy name for folder). Each bucket has a linkedList. In that linkedList our keys are stored. HashMap locates the correct linkedList for each key by applying hashCode() method and after that it iterates through all elements of that linkedList and applies equals() method on each of these elements to check if that element is already contained there. No duplicate keys are allowed.

When we put something inside a HashMap, the key is stored in one of those linkedLists. In which linkedList that key will be stored is shown by the result of hashCode() method on that key. So if key1.hashCode() has as a result 4, then that key1 will be stored on the 4th bucket of the array, in the linkedList that exists there.

By default hashCode() method returns a different result for each different instance. If we have the default equals() which behaves like == which considers all instances in memory as different objects we don't have any problem.

But in our previous example we said we want Person instances to be considered equal if their ages and names match.

Person person1 = new Person("Mike", 34);
Person person2 = new Person("Mike", 34);
System.out.println ( person1.equals(person2) ); --> will print true!

Now let’s create a map to store those instances as keys with some string as pair value

Map<Person, String> map = new HashMap();
map.put(person1, "1");
map.put(person2, "2");

In Person class we have not overridden the hashCode method but we have overridden equals method. Since the default hashCode provides different results for different java instances person1.hashCode() and person2.hashCode() have big chances of having different results.

Our map might end with those persons in different linkedLists.

This is against the logic of a HashMap.

A HashMap is not allowed to have multiple equal keys!

But ours now has and the reason is that the default hashCode() which was inherited from Object Class was not enough. Not after we have overridden the equals() method on Person Class.

That is the reason why we must override hashCode() method after we have overridden equals method.

Now let’s fix that. Let’s override our hashCode() method to consider the same fields that equals() considers, namely age, name

public class Person {
private Integer age;
private String name;

..getters, setters, constructors
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age &&
name.equals(person.name);
}
@Override
public int hashCode() {
int prime = 31;
return prime*Objects.hash(name, age);
}

PS: In the hashCode() method, we used a prime value (you can use any other values). However, it is suggested to use prime numbers as this in order to produce less collisions.

Now let’s try again to save those keys in our HashMap:

Map<Person, String> map = new HashMap();
map.put(person1, "1");
map.put(person2, "2");

person1.hashCode() and person2.hashCode() will definitely be the same. Let's say it is 0.

HashMap will go to bucket 0 and in that LinkedList will save the person1 as key with the value “1”. For the second put HashMap is intelligent enough and when it goes again to bucket 0 to save person2 key with value “2” it will see that another equal key already exists there.

So it will overwrite the previous key. So in the end only person2 key will exist in our HashMap.

Now we are aligned with the rule of Hash Map that says no multiple equal keys are allowed!

Keep in mind however, un-equal instances may have same hashcode and equal instances should return same hashcode.

--

--