Home » Java » Core Java » How HashMap works in java

About Arpit Mandliya

Arpit Mandliya
I am java developer at Tata Consultancy Services Ltd. My current area of interest are J2EE,web development and java design patterns. I am technology enthusiast trying to explore new technologies. In spare time,I love blogging.

How HashMap works in java

Most common interview questions are “How HashMap works in java”, “How get and put method of HashMap work internally”. Here I am trying to explain internal functionality with an easy example. Rather than going through theory, we will start with example first, so that you will get better understanding and then we will see how get and put function work in java.
Lets take a very simple example. I have a Country class, we are going to use Country class object as key and its capital name(string) as value. Below example will help you to understand, how these key value pair will be stored in hashmap.
 
 
 

1. Country.java

package org.arpit.javapostsforlearning;
public class Country {

 String name;
 long population;

 public Country(String name, long population) {
  super();
  this.name = name;
  this.population = population;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public long getPopulation() {
  return population;
 }
 public void setPopulation(long population) {
  this.population = population;
 }

 // If length of name in country object is even then return 31(any random number) and if odd then return 95(any random number).
 // This is not a good practice to generate hashcode as below method but I am doing so to give better and easy understanding of hashmap.
 @Override
 public int hashCode() {
  if(this.name.length()%2==0)
   return 31;
  else 
   return 95;
 }
 @Override
 public boolean equals(Object obj) {

  Country other = (Country) obj;
   if (name.equalsIgnoreCase((other.name)))
   return true;
  return false;
 }

}

If you want to understand more about hashcode and equals method of object, you may refer hashcode() and equals() method in java

2. HashMapStructure.java(main class)

import java.util.HashMap;
import java.util.Iterator;
  
public class HashMapStructure {
  
    /**
     * @author Arpit Mandliya
     */
    public static void main(String[] args) {
          
        Country india=new Country("India",1000);
        Country japan=new Country("Japan",10000);
          
        Country france=new Country("France",2000);
        Country russia=new Country("Russia",20000);
          
        HashMap<country,string> countryCapitalMap=new HashMap<country,string>();
        countryCapitalMap.put(india,"Delhi");
        countryCapitalMap.put(japan,"Tokyo");
        countryCapitalMap.put(france,"Paris");
        countryCapitalMap.put(russia,"Moscow");
          
        Iterator<country> countryCapitalIter=countryCapitalMap.keySet().iterator();//put debug point at this line
        while(countryCapitalIter.hasNext())
        {
            Country countryObj=countryCapitalIter.next();
            String capital=countryCapitalMap.get(countryObj);
            System.out.println(countryObj.getName()+"----"+capital);
            }
        }
  
  
} 
</country></country,string></country,string>

Now put debug point at line 23 and right click on project->debug as-> java application. Program will stop execution at line 23 then right click on countryCapitalMap then select watch.You will be able to see structure as below.
 
HashMapStructure1
Now From above diagram, you can observe following points

  1. There is an Entry[] array called table which has size 16.
  2. This table stores Entry class’s object. HashMap class has a inner class called Entry.This Entry have key value as instance variable. Lets see structure of entry class Entry Structure.
  3. static class Entry implements Map.Entry
    {
            final K key;
            V value;
            Entry next;
            final int hash;
            ...//More code goes here
    }
  4. Whenever we try to put any key value pair in hashmap, Entry class object is instantiated for key value and that object will be stored in above mentioned Entry[](table). Now you must be wondering, where will above created Enrty object get stored(exact position in table). The answer  is, hash code is calculated for a key by calling Hascode() method. This hashcode is used to calculate index for above Entry[] table.
  5. Now, If you see at array index 10 in above diagram, It has an Entry object named HashMap$Entry.
  6. We have put 4 key-values in hashmap but it seems to have only 2!!!!This is because if two objects have same hashcode, they will be stored at same index. Now question arises how? It stores objects in a form of LinkedList(logically).

So how hashcode of above country key-value pairs are calculated.

Hashcode for Japan = 95 as its length is odd.
Hashcode for India =95 as its length is odd
HashCode for Russia=31 as its length is even.
HashCode for France=31 as its length is even.

Below diagram will explain LinkedList concept clearly.
 
HashMapStructure2
So now if you have good understanding of hashmap structure,Lets go through put and get method.

Put :

Lets see implementation of put method:

/**
  * Associates the specified value with the specified key in this map. If the
  * map previously contained a mapping for the key, the old value is
  * replaced.
  *
  * @param key
  *            key with which the specified value is to be associated
  * @param value
  *            value to be associated with the specified key
  * @return the previous value associated with <tt>key</tt>, or <tt>null</tt>
  *         if there was no mapping for <tt>key</tt>. (A <tt>null</tt> return
  *         can also indicate that the map previously associated
  *         <tt>null</tt> with <tt>key</tt>.)
  */
 public V put(K key, V value) {
  if (key == null)
   return putForNullKey(value);
  int hash = hash(key.hashCode());
  int i = indexFor(hash, table.length);
  for (Entry<k , V> e = table[i]; e != null; e = e.next) {
   Object k;
   if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
    V oldValue = e.value;
    e.value = value;
    e.recordAccess(this);
    return oldValue;
   }
  }

  modCount++;
  addEntry(hash, key, value, i);
  return null;
 }

now lets understand above code step by step

  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.If you want to learn more about hash() function, you can refer hash and indexFor method in hashmap.
  3. indexFor(hash,table.length)  is used to calculate exact index in table array for storing the Entry object.
  4. As we have seen in our example, 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.
  • If there is no element present at that index which we have just calculated then it will directly put our Entry object at that index.
  • If There is element present at that index then it will iterate until it gets Entry->next as null.Then current Entry object become next node in that linkedlist
  • What if we are putting same key again, logically it should replace old value. Yes,it will do that.While iterating it will check key equality by calling equals() method(key.equals(k)), if this method returns true then it replaces value object with current Entry’s value object.

 Get:

Lets see implementation of get now:

/**
  * Returns the value to which the specified key is mapped, or {@code null}
  * if this map contains no mapping for the key.
  *
  * <p>
  * More formally, if this map contains a mapping from a key {@code k} to a
  * value {@code v} such that {@code (key==null ? k==null :
  * key.equals(k))}, then this method returns {@code v}; otherwise it returns
  * {@code null}. (There can be at most one such mapping.)
  *
  * </p><p>
  * A return value of {@code null} does not <i>necessarily</i> indicate that
  * the map contains no mapping for the key; it's also possible that the map
  * explicitly maps the key to {@code null}. The {@link #containsKey
  * containsKey} operation may be used to distinguish these two cases.
  *
  * @see #put(Object, Object)
  */
 public V get(Object key) {
  if (key == null)
   return getForNullKey();
  int hash = hash(key.hashCode());
  for (Entry<k , V> e = table[indexFor(hash, table.length)]; e != null; e = e.next) {
   Object k;
   if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
    return e.value;
  }
  return null;
 }

As you got the understanding on put functionality of hashmap. So to understand get functionality is quite simple. If you pass any key to get value object from hashmap.

  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.

Key points to Remeber:

  • HashMap has a inner class called Entry which stores key-value pairs.
  • Above Entry object is stored in Entry[ ](Array) called table
  • An index of table is logically known as bucket and it stores first element of linkedlist
  • Key object’s hashcode() is used to find bucket of that Entry object.
  • If two key object ‘s have same hashcode , they will go in same bucket of table array.
  • Key object ‘s equals() method is used to ensure uniqueness of key object.
  • Value object  ‘s equals() and hashcode() method is not used at all

 

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

37 comments

  1. Hi Arpit,

    This is by far the best explanation that I have seen for HashMap. The pictures and clear explanation makes it very easy to understand. Thanks a ton.

    Regards,
    Raghu

  2. Simple and great example.

    Regards
    Sudhakar

  3. One of the best and easy explainations to understand hashmaps…

  4. This is the best example. I have seen. Superb!.

  5. Hi,

    The explanation is really good but I had one doubt regarding its capacity. Suppose I initially give a fix size to the HashMap constructor and later if I want to increase the size then how will it do it??

  6. Very nicely explained. Really apprecite the effort.

    Thanks,
    Satyajit

  7. Hi Arpit,

    This is the best explanation I have seen for the Hashmap so far! Thank you so much.

  8. Hi Man,

    Really awesome explanation. Great job and thankss……………

  9. Hi..
    Precise and perfect.. undoubtedly the best explanation I’ve seen for the internal implementation of HashMap.

    Thank you

  10. Great explanation!!!

  11. Nicely explained..appriciate it. have a doubt that how it checks for the table(Entry[]) capacity

  12. Cool Explanation dude

  13. Have been using debug for some years . Didn’t realize it can teach us how the data structures are stored internally. Well explainded..Kudos

  14. Very Well explained..!! thanks Helped a lot..!!

  15. Explained full concept in detail. Helped to understand internal data structures. Thanks for the post!!!.

  16. Hi Arpit,

    First of all thanks for amazing demonstration! I have a question :)

    I was running the code from your example and found that in my case there are not two linked list for different hashcode values. Rather I have a single linkedlist in 16th cell of the “table” and all the entries are there in that very linked list. Can you shred some light on this matter please?

  17. Good explanation and great job!

  18. Your explanation is superb brother.

  19. Your explanation is superb bro. Thanks

  20. Thanks… I got the best explanation of HashMap implementation over here.

  21. Very good Explanation, Thanks Friend

  22. Definitely quite informative and really easy to understand

  23. Your exlanation is excelent. Thanks

  24. Good Exlaination. Thanks

  25. Well explained with screen shots.

  26. Thanks good article

  27. hi Arpit,
    thanks a ton. you clear my doubt regarding the hashmap working.

    thanks..
    rick

  28. Well written. This is the first time I have read a technical blog which is very easy to understand the tough concepts!

  29. very nice explanation.
    I knew the working of HashMap before, but after reading this I am very confident about HashMap now.
    Keep going dude.

  30. hashCode and equals() method play an important role in how values are stored and retrieved from a hashMap. See an example about that here – http://netjs.blogspot.com/2015/07/why-wait-notify-and-notifyall-called-from-synchronized-java.html

  31. Hi Arpit,

    Awesome explanation, thanks for your time to do a such a great explanation.

  32. The best explanation I ever read. GreatThanks!

  33. Nice Explanation bro !!!!!

  34. Hi

    very Well explained i got cleared my all doubts, thanks a lot

  35. nice explanation

Leave a Reply

Your email address will not be published. Required fields are marked *

*


Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.