How HashMap Works in Java – hashcode and equals method contract

Here we will see How Hashmap works in java and what is  hashcode and equals method contract?.If you are a beginner i hope this post will help you understand this concept.

           Here I will cover following things

1. How HashMap Works in Java?

2. Why we need override hashcode and equals method if can custom object as Key?.

3.What will happen if two objects has same hashCode?

1.How Hashmap works?

We can simply say  On principle of Hashing. It is important to understand How get() and put() methods works in hashmap.  

                 When we use put() method it  Calculate  hashcode  of key  If  basket  with that  hashcode  is present then use the  equals  method on the key search the keys that basket to determine if the element is to be added or replace and If not there then create new basket (rehashing) and add that element to that.   When we use get() methods  it get the  hashcode  of key and go  to that basket  Iterate using  equals  on the key will return you that element from that basket.

2. Why we need override hashcode and equals method?

Create class with and



Following will be the output for the HashMapExample Program.Even Person Object is there in Hashmap still if you call get method by passing  Person Object as Key it returns null.See the Output below.


Person  HashCode = 11533424

Student hashCode = 31866429


Value = null
I hope now you got it why we need to override hashCode() and equals() methods.Let’s see if you only implement hashCode() in Person Class.
Following will be the Output.Now hashCode is same but object equality check is missing here  so it will return null.So you must override equals() methods along with hashCode() method.

 Person HashCode = -1825605562

Student hashCode = 16795905


Value = null
If you Override Both methods then result will be

Person HashCode = -1825605562

Student hashCode = 28904249


Value = Employee1
I hope you understood about Why we need to override equals() and hashCode() methods when we use user defined or custom object as Key in Hashmap.

3.What will happen if two Objects has Same hashCode()?

 Since hashcode is same, bucket location would be same and collision will occur in HashMap, Since  HashMap  use LinkedList to store object.And Important point to mention is that HashMap in Java stores both key and value object as  Map.Entry  in bucket.The bucket  is  the linked list, effectively. The  table  array is an array of  Entry  elements, and each  Entry  is a linked list, in that each entry knows about the next one in the list, until you reach the end when the  next  reference is null. The  for  loop you’ve shown iterates over the linked list.

It’s not a  LinkedList  as in a  java.util.LinkedList  – it’s a separate (simpler) implementation just for the map.

See the get() method code

About mohan

This is Mohan Jayapalan author of TechPages Blog.I am a part time blogger and Currently working for private Software Concern.

© Copyright 2010-2016 · All Rights Reserved · Powered by WordPress ·