HashMap example with hashCode and equals method in Java

In this tutorial, we will see the importance of hashCode and equals method while writing code by using HashMap. We will see first what the default behaviour of these methods and later will see how to override these methods.

Both hashCode() and equals() method are defined in Java.lang.Object class.

hashCode () : public navtive int hashCode();

It returns the hash code value for the object. This method returns the unique integer value of a given object. That returned integer value is used to find the bucket location, when you are trying to insert that object in Hash Map/Hash Table.

equals ():  

publicboolean equals(Object obj) {

return (this == obj);


Equals method returns Boolean value. This method is used to compare the 2 objects. By default it compares the object reference of two objects to check their equality.

Example on HashMap with equals and hashCode methods.


First 3 lines of output prints from the below code.

for (Entry<Employee, String> entry : map.entrySet())


System.out.println(entry.getKey().getName() +”-“+entry.getKey().getid() + “-” + entry.getValue());


Next, employee3 has printed from below code.


Here we have passed e3 object as a key to the map, and we got employee3 as value from map.

Till now it’s fine.

See last line of output. It’s printed null from below code.

System.out.println(map.get(new Employee(“krishna”, 1002)));

Here we are creating new employee object by passing values to the employee constructor and we are expecting the employee3 as the output. But it returned null. See below why its returned null.

We know that equals method checks the equality of references objects. Not their contents. So we need to override the equal’s method to check the equality of the object contents.

We know that hashCode returns unique integer value for a given object and is used to find the bucket location.

We passed new Employee(“krishna”,1002) as a key to the HashMap get method.

What HashMap’s get method does here?

In short, it first creates the hashcode from the key object (new Employee(“krishna”,1002)).

Then it finds the hash map value object based on this hashcode. It will look for the hash map value object based on hashcode.  Since we passed object as new Employee(“krishna”,1002), it creates new hashcode which is not there in the Entry table.

If you pass the key object as e3, it finds the hashcode and try to see in the table to find the value of a map. Since we stored the map value as map.put(e3, “employee3″);, so it found the result in the Entry table and it returned employee3.

But if you pass new Employee(“krishna”,1002) as a key, then it will return null. So we should override the hashCode method.

Uncomment the equals and hashcode methods code in above program and execute it. We will get output as below.

hashCode() – HashCode is generated based on the Employee name and id. So it will gives the same hash code for the below object keys.

new Employee(“krishna”,1002) object

e3 object

equals() – equality of objects has been done based on Employee name and id. So it will gives the true for the below object keys.

new Employee(“krishna”,1002) object

e3 object

You can override these methods easily from Eclipse. Right click on code, Click on Souce, and Click on Generate HashCode and equals.

Please post comments, if you have any doubts. I will explain.

Posted in collections and tagged , , , .


  1. Many thanks for creating the effort to talk about this, I feel strongly about this and love studying a great deal more on this matter. If feasible, as you gain knowledge, would you mind updating your website with a great deal more information? It’s extremely helpful for me.

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="">