Java- How hash map works in java or How get() and put() method works internally - JavaTechWorld

Tuesday, 16 May 2017

Java- How hash map works in java or How get() and put() method works internally

    0

Hash Map in Java- Java HashMap class implements the map interface by using a hashtable. It inherits AbstractMap class and implements Map interface. it store value in key, value pair format. HashMap is known as HashMap because one ot it's technique i.e hashing.

HashMap
HashMap Example-
import java.util.*;class TestCollection13{
public static void main(String args[]){
 HashMap<Integer,String> hm=new HashMap<Integer, String>();  //line 4
hm.put(“ajay”,123456789);  //line 5
hm.put(“abhishek”,987654321);//line 6
hm.put(“abhinav”,234156748);//line 7
for(Map.Entry m:hm.entrySet()){
            System.out.println(m.getKey()+" "+m.getValue());
        }
    }
}  
Internal Implementation of HashMap-
Hashing- Hashing is a technique which convert large string into fixed length value it is alpha numeric value.
Example-
public class HelloWorld{
    public static void main(String []args){
        String s="abc";
        System.out.println(s.hashCode());
    }
}
In object class java has given equals method which says objects that are equal must have the same hash code within a running process here hashcode of string abc is 96354 this value might be different in your system.
To learn how to override equals and hascode method in java Click Here

If you check the above diagram HashMap internally implements hashtable and each index of and hastable has it’s own linked list implementation because there is a possibility object class hashcode method may return duplicate hashcode.

HashMap has an inner class Entry, which looks like this:

static class Entry<K ,V> implements Map.Entry<K ,V>
{
    final K key;
    V value;
    Entry<K ,V> next;
    final int hash;
    //Some code here
}

How put operation work in java- Using above algorithm we will understand how put() method internally work in HashMap.
Before inserting values in the array index there are some check which is performed by java program internally.
  • First of all, key object is checked for null. If key is null, value is stored in table[0] position. Because hash code for null is always 0.
  • Then on next step, a hash value is calculated using key’s hash code by calling its hashCode() method. This hash value is used to calculate index in array for storing Entry object. 
  • Now index for function is called to calculate exact index position for storing the Entry object.
  • Here comes the important concept two object can have same hash code value and these value will store in same array location because of each  index has it’s own linked list implementation. 
Note:-  Java HashMap allows null key, which always goes to index 0 as hash of 'null' is zero.

How get operation work in java:- In get operation we will call get('varun') then we will find hash of the key after that we will calculate index and check in which index value exist we will compare both hashcode and key at particular index once this is verified value will be returned.
Java Hash Map Design Diagram


Hope you have like this blog kindly suggest on the comment box for any other query contact me Here


© 2014 JavaTechWorld. Designed by Bloggertheme9
Powered by Blogger.