Monday, 16 June 2014

How HashMap works internally in Java?

One of the most important question of the core java interviewers is How
hash map works in java or internal.implementation of hashmap. Most of
the candidates rejection chances increases if the candidate do not give
the satisfactory explanation . This question shows that candidate has
good knowledge of Collection . So this question should be in your to do
list before appearing for the interview .
HashMap works on the principle of Hashing . To understand Hashing , we should understand the three terms first i.e Hash Function , Hash Value and Bucket .

What is Hash Function , Hash Value and Bucket ?

hashCode() function which returns an integer value is the Hash function. The important point to note that , this method is present in Object class ( Mother of all class ) .
This is the code for the hash function(also known as hashCode method) in Object Class :

public native int hashCode();

The most important point to note from the above line : hashCode method return int value .
So the Hash value isthe int value returned by the hash function .

What is bucket ?
A bucket is used to store key value pairs . A bucket can have multiple
key-value pairs . In hash map, bucket used simple linked list to store
objects .
Before start with internal working of HashMap,first you should know this thing,1) Two unequal object may return same hashcode.2) When two objects are equal by equals(), then they must have same hashcode.

There are two main methods used for storing and retrieving values from HashMap.
1.put() method to put /store values in HashMap
2.get() method to retrieve data/values from HashMap

1) Key object is checked for null. If key is null then it will be stored at table[0] because hashcode for
null is always 0.
2) Key object’s hashcode() method is called and hash code is calculated. This hashcode is used to find
index of array for storing Entry object. It may happen sometimes that, this hashcode function is
poorly written so JDK designer has put another function called hash() which takes above calculated
hash value as argument.
3) indexFor(hash,table.length) is used to calculate exact index in table array for storing the Entry
object.
The indexFor will return the index of table array, the index calculation is based on hashcode of key and length of table array. This will do the and operation as ,

staticintindexFor(int h,int length){return h &(length-1); }

4) If two key objects have same hashcode(which is known as collision) then it will be stored in form
of linkedlist.So here, we will iterate through our linkedlist.

1) Key object is checked for null. If key is null then value of Object resides at table[0] will be returned.
2) Key object’s hashcode() method is called and hash code is calculated.
3) indexFor(hash,table.length) is used to calculate exact index in table array using generated hashcode for getting the Entry object.
4) After getting index in table array, it will iterate through linkedlist and check for key equality by calling equals() method and if it returns true then it returns the value of Entry object else returns null.

What happens if two keys has same hashCode?

If multiple keys has same hashCode, then during put() operation collision had occured, which means multiple Entry object stored in a bucket location. Each Entry keep track of another Entry, forming a linked listdata structure. Now , if we need to retrieve value object in this situation, following steps will be followed :
1) Call hashCode() method of key to find bucket location.
2) Traverse through linked list, comparing keys in each entries using keys.equals() until it return true. So, we use equals() method of key object to find correct entry and then return value from that.

Key Notes:--

Data structure to store Entry objects is an array named table of type Entry.

A particular index location in array is referred as bucket, because
it can hold the first element of a LinkedList of Entry objects.

Key object’s hashCode() is required to calculate the index location of Entry object.

Key object’s equals() method is used to maintain uniqueness of Keys in map.

Value object’s hashCode() and equals() method are not used in HashMap’s get() and put() methods.

Hash code for null keys is always zero, and such Entry object is always stored in zero index in Entry[].

In the above case, While creating new class object you must override the hashCode() and equals() method. While creating new object it will create new hash code that won't match with existing hash code so it will return null

One of the most important question of the core java interviewers is How hash map works in java or internal.implementation of hashmap. Most of the candidates rejection chances increases if the candidate do not give the satisfactory explanation . This question shows that candidate has good knowledge of Collection . So this question should be in your to do list before appearing for the interview .Thanks for helping me to understand basic concepts. As a beginner in Java programming your post help me a lot.Thanks for your informative article.Thanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.java training in chennai | java training institutes in chennai