#### Latest posts by Prasad Kharkar (see all)

- How do neural networks learn? - April 17, 2018
- Artificial Neural Networks - April 17, 2018
- The Neuron in Deep Learning - April 17, 2018

Hi, this article will deal with working of HashMap in Java. We all know that HashMap is a data structure which is used to store key and value pairs of object in java. It is dynamic and can add values without specifying size of map. This article will try to explain working of hashmap in java in aspect of its dynamic size and hashing.

# Working of HashMap in Java:

Working of HashMap in Java is depending on multiple aspects. This article will deal with how it can grow in size store hashes. There are certain factors to consider in understanding working of HashMap in Java.Let us understand them a bit

**Initial Capacity**: This is the capacity of hashmap to store number of key value pairs when it is instantiated.**Load Factor**: A parameter responsible to determine when to increase size of hashmap**Size**: number of key value pairs in hashmap.**Threshold value**: When number of key value pairs is more than threshold value, then hashmap is resized.

Now, if you see the HashMap source code in java, you’ll see that initial capacity is 16 by default and the load factor is 0.75. Based on these parameters, threshold value is calculated where threshold value is the product of load factor and size. So threshold value is 12 for a newly created instance of HashMap. Let us try something out.

# Example to Understand Working of HashMap in Java:

Simply create a HashMap and try to put objects in it using a for loop.

1 2 3 4 |
Map map = new HashMap(); for (int i = 0; i < 20; i++) { map.put(i, i); } |

Let us understand working of HashMap in java

- When new HashMap() is instantiated, load factor is initialized to 0.75 and initial capacity is 16. Threshold is still 0.
- When first key is put in hashmap, then threshold value is calculated which will be product of initial capacity and load factor i.e. 16 * 0.75 = 12.
- Now as the for loop keeps on adding key value pairs in HashMap, as soon as number of key value pairs, i.e. size of hashmap exceeds threshold value, HashMap is rehashed and capacity becomes approximately twice the previous size. Let us see what happens to each parameter in the table below.

Iteration | Size | Capacity | Threshold |

0 | 0 | 16 | 0 |

1 | 1 | 16 | 12 |

2 | 2 | 16 | 12 |

3 | 3 | 16 | 12 |

4 | 4 | 16 | 12 |

5 | 5 | 16 | 12 |

6 | 6 | 16 | 12 |

7 | 7 | 16 | 12 |

8 | 8 | 16 | 12 |

9 | 9 | 16 | 12 |

10 | 10 | 16 | 12 |

11 | 11 | 16 | 12 |

12 | 12 | 16 | 12 |

13 | 13 | 32 | 24 |

14 | 14 | 32 | 24 |

Please note that capacity of hashmap became twice as soon as size of hashmap exceeded the threshold value. So this will go one continuously. If size becomes 25 then capacity becomes 64 and threshold becomes 48. This keeps going on.

I hope this article helped understand the working of HashMap in Java.

Thanks simple and comphrensive explanation.

Thank you Pramod for reading. Please keep providing feedback and I”ll definitely improve myself.

Hi article is very nice, clearly explains about HashMap in Java for the beginners.