HashMap Part-2

This is the continues part of HashMap Part-1. If you have not gone through this part we suggest you please go through this part which explains the basic structure of HashMap.

In this part, we will cover how HashMap’s put( key , value ) and get ( key ) methods works.

Put( key , value)

The following are the steps that are executed in put method.

  1. Identify the hashCode of the Key
  2. Based on hashCode identify bucket number of the HashMap
  3. equals check of each Entry of identified Bucket’s link list
  4. Perform operation based on the equals return value

HashMap’s put method has two parameters, key and value. As method execution, it first calculates the hashcode of the key. Based on Hashcode the next step is to identify bucket numbers. Each bucket has a link list of Entry class which is an inner class of HashMap.Once HashMap has bucket number it will first check if the bucket has a link list is empty it will add new key-value pair as Entry, Which is an inner class of HashMap. If the link list is not empty then HashMap checks with each entry node of the link list. This check operation over here is done by boolean equals(Object key) method of the key. The first node of the link list’s equals() method will be executed. If equals returning false then it next node’s equals method will get executed. If the next node is not present then new Entry node will get inserted at the end of the link list. If the equals method is returning true then-current Entry nodes value will get replaced with the new value.

Example of put operation:

Here we are trying to Put(“Tom Cruise ”,”XXXX12345565”) Entry in empty HashMap. Based on key –> “Tom Cruise” we get hashCode=575754 and based on that we got bucket index as 2.

As you can see, This is the final image of the HashMap. As you can see earlier bucket index 2’s link list is empty, So equals did not come into the picture. So it will add in the link list as first node Entry.

The same explanation applies for the Put(“Johnny Depp ”,”XXXX12345565”)

For Put(“Brad Pitt”,”XXXX12345565”) we got hashcode=3220 and bucket index as 1. If we can bucket index already have a link list which not empty. So it will pick the first node to execute equals with that nodes key. As you can see in the image it returns false. Then we will go to the next node but here the next node is null so we will add a new entry at the end of the link list.

The next stage of the HashMap after performing the put operation of (“Brad Pitt”,”XXXX12345565”) into the HashMap.

For Put(“Brad Pitt”,” XXX999999999 ”) we got hashcode=3220 and bucket index as 1. If we can bucket index already have a link list which not empty. So it will pick the first node to execute equals with that nodes key. As you can see in the image it returns false. Then we will go to the next node. We will perform same operation on the next node. Here it returns true that means key matched and it already exists in the HashMap. So key will remain the same but Value will get replaced with the new value.

The above image explains how keys equals returns true for the current node.

The next stage of the HashMap after performing the put operation of (“Brad Pitt”,” XXX999999999 ”) into the HashMap.

This is How put opration works in HashMap.

Get( key )

The following are the steps that are executed in the get method. It is having most of the steps are the same as put operation.

  1. Identify the hashCode of the Key
  2. Based on hashCode identify bucket number of the HashMap
  3. equals check of each Entry of identified Bucket’s link list
  4. return that value if equals return true

Example Of Get operation.

Here we are trying to get the value of (“Brad Pitt”) key. We get hashCode value as 3220 and based on it we get bucket index as 1. As you can see bucket 1’s link is not empty so equals of the first node will get executed. It returns false so the next node of the list will get selected for the equals operation. If the next node is null then it will return null value. But here we have next node.

As you can see next nodes equals return true so its value will get the return as a result of the get operation.

That’s it for the put and get operation of the Hashmap.

Happy learning 🙂

Rahul Chudasama
Leave a Reply