Internal working of Java HashMap

Internal working of Java HashMap

In this article, we are going a little deep into the Hashmap. We will follow the onion method to explore things. So let's ask the question What internally happens when JVM create Hashmap

HashMap map= new HashMap<>()

When we ask JVM to create HashMap then JVM does is in the heap memory create the 16 buckets

Screenshot 2022-01-04 at 6.57.19 AM.png

This analogy is similar to the water, buckets in the program we consider as data buckets and we can consider as Arrays

So you all must have a question that, We can store more than 16 elements in the Hashmap. so how its storing, what if there are more than 16 items? Yes, we can store more than 16 items and how does it work? here the concept of load factor comes into the picture. Load factor concept is saying that, if Hashmap reach more than 75% of its capacity then it double the existing capacity

So 12 buckets get full and we are trying to put the 13th item then its capacity will get double Before diving into the internals let's see what is exactly is the bucket and how data is getting store in the buckets?

So friends bucket is nothing but the LinkedList

Screenshot 2022-01-04 at 7.13.49 AM.png

You all might know how the link list work but let's revise it quickly once because we need it ahead Link list stores the data and in the form for Node this node consists of 2 things one is data and another one is a pointer to the next Node in the chain

Now we can imagine How bucket of HashMap looks like

Screenshot 2022-01-04 at 7.24.10 AM.png So we can see here How bucket can contain the link list nodes and Now you have an idea how memory structure looks like As we already know that hashmap is used to store the key-value pair

map.put("key","Value") Let's try to put some key-value pairs in the hashmap

map.put("FB","A") here "FB" is key and "A" values, So when we try to put the key-value pair then JVM will do, it takes the key (Remember key value should be an object not primitives values) and key must be an object it cannot be a primitive type JVM will get the Hashcode. you must have a question about where this hashcode comes from .so it is available in the Object class that is the reason we cannot use the primitive here as a key So from hashcode, JVM will calculate the bucket index using the below formula

BucketIndext=Hashcode & (Length -1)

in our case Hashcode "FB"= 2236 BucketIndex= 2236 & (16-1) = 12

(we put 2 items in the bucket as you can see in the example)

Screenshot 2022-01-04 at 7.55.40 AM.png

Hash collision

Now let's go for one more example map.put("Ea","C") So in the example, we will see the has collision example So JVM get the hashcode for "Ea" and it is the same as "FB" which will go in the 12th bucket So we already have the one key-value pair present in the 12th index bucket This situation is called the HashCollision When JVM tries to put the item in the bucket and it already has one or more nodes then this condition is called the hash collision. So in that case JVM checks if there same key present or not. If the same key exists then JVM will replace the existing node with new key-value pair if a new key is not present then new key-value pair is added as the next node to the bucket

Screenshot 2022-01-04 at 8.09.20 AM.png

Lets. summarize the process in the bellow diagram

Screenshot 2022-01-04 at 8.19.16 AM.png

Lets see the get the method of HashMap

so before going to the getting the item from the HashMap we should see how the searching work in the link list

suppose we have 4 items in the link list

so we want to find the item-3 then we can't go to the 3rd item we have to go one by one from the beginning of the link list and that is the biggest disadvantage of the link list and this is how search works in the link list and you can see that we have to traverse one by. one node till we find the item which we want to search so it has. performance headache

Now, let's see how to get the key-value pair from Hashmap WE know get metho to give the value of HashMap which is already added

map.get("EA")

GEtting The element from the HashMap happens very very fast no matter how much element it contains which is the biggest advantage of HashMap The time complexity for getting the element is O(1) meaning constant it doesn't affect the size of the hashmap

Now How its work So JVM get the Hashcode of "Key" in our example "Ea" and then it calculate the index of the bucket using the same formula Hashcode & (length-1) which is 12 So we have 2 items present in the 12th bucket then JVM will go to the first node and check whether "Ea" matches to "FB" and then search till it finds and it uses the equal method to compare the keys and once find then return the value.

Generally, every bucket will have only one node and Hash. collision is a very rare scenario and in that case, the bucket can have multiple nodes So if the bucket contains multiple nodes then performance will be degraded because JVM has. to search through the link list

JAVA 8 Enhancement in HashMap In java 8 Java people identifies the situation and they made some awesome changes in the search item in the HashMap Consider the scenario we have some key-value pair with the same hashcodes so they will gon in the same bucket and form a long link list so java people change this link list to the binary balance tree once the certain thresh hold passes. The time complexity of search in the binary is O(log(n)) which is way better than the link list which O(n)

I hope you learn something new from this article. Thank you for reading