Internal Architecture Of Hash Map: Now days it is very popular

advertisement
Internal Architecture Of Hash Map:
Now days it is very popular interview question for any senior or mid level interviews. Anybody who has worked in
java, knows or heard about HM. Then what really makes this question popular? There are some resign to answer
this.
1) The internal/ depth architecture of Hash Map is something special that helps to crack the interviewer as it related to
depth of hashcode, equals method. == operators etc.
2) Concept of static nested class, arrays, linkedlist also coming around this.
3) And also as java concurrent package introduced concurrenthashmap, so to go to this HM is the good question to
start with.
So based on my experience I am putting some points which describes how HM works?
From high level view, one can say hashmap is kind of a data structure where user can put a key/value pair so that we
can retrieve the value by passing the key in future. So what internal data structure and mechanism to perform this
task.
Let's go through put method…
put method takes two parameters as arguments a) key as generic object and b) value as generic object.
There are 2 properties inside HM that stored some default initial values like DEFAULT_INITIAL_CAPACITY =
16, and DEFAULT_LOAD_FACTOR = 0.75f
And the internal bucket has declared as transient Entry[] table. where Entry is the static class of HM that implements
Map.Entry interface having 4 properties 1) one for key object, 2) one for Value object , 3) one for hash and 4) and
lastly a reference to itself.
The above picture represents one Entry object
So when you declare a Hashmap object with default constructor then size of this Entry array object will be equal to
DEFAULT_INITIAL_CAPACITY and a special value called threshold value is calculated by following manner.
threshold = DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR.
Each index of this table known as bucket. So initially HM contains 16 buckets if created by default constructor.
Lets assume my Hashmap declare as following
HashMap<String,String> mymap = new HashMap<String,String>();
mymap.put("key1","value1");
What happens internal to the above statement. Assuming the key is not null.
1) does its own mechanism to calculate the hash of the key object
int hash = hash(key.hashCode());
2) Put some algorithm to calculate the index of the bucket
int i = indexFor(hash, table.length);
It is possible that more than one entry can find the same index. Then how this elements will store in the same index.
Following some step is the answer to this.
Remember single linked list is nothing but a chain of elements keeping the address/reference to the next element. So
hear each bucket of HM i.e table[i] is treated as one one single linked list.
Let's assume we have 12 buckets and we call put method for 1st time and depending upon the hash code the index
calculated as 3. So 1st element goes into table[3] and the new entry looks like following pic.
2nd entry to map mymap.put("key2","value2");
Again by calculating above 2 steps let the index be calculated as 5 so this new entry will go to table[5].
3rd entry to map mymap.put("key3","value3");
Again by calculating above 2 steps let the index be calculated as 3 so this new entry will go to table[3]. But table[3] is
already filled by 1st entry (key1). As I told you that every bucket i.e table[i] is one one linked list so this 3 rd entry will be
stored in the 4th bucket i.e table[3] and it will be stored by inserting 1 st of the buckets linked list. The newly created
entry will be like following pic.
In this way entry goes on to hash map. At each entry the size of HP (number of elements in HM) is compared with a
value called threshold value. Once the size equals to threshold value then HM tries to resize the length of the entry
table by doubling it. And this continues…
Following picture demonstrates the status of the Hash Map after inserting 3 above values.
0
1
2
3
HASH
3
KEY
3
VALUE
3
REF TO
NEXT
HASH
2
KEY
2
VALUE
2
NULL
HASH
1
KEY
1
VALUE
1
NULL
4
5
6
7
8
9
10
What happens when a duplicate entry comes?
Ans: So after getting the proper index, HM traverses the whole linked list to search whether the same key is stored or
not. If it founds such key then it simply overwrite the value in the existing key or else it inserts the key entry in the
first of the linked list.
What happens when get method called?
Ans: get method takes 1 parameter and i.e the key object. Like in put method, get also follows the same steps to get
the index of the key. Once it gets the index it traverse the corresponding linked list and tries to find the key. If it gets
then returned the value object of the Entry or simply it returns null.
How Hash map finds whether key is duplicate?
After getting the index, it starts searching the corresponding list and compares 3 values.
a)
Hash of the current key with hash of the entry object
b)
Use == operator with current key object and Entry’s key object
c)
call equals method with both the key objects
if(a && (b || c)) gets true, then it assumes that the key is duplicate.’
Download