How HashMap works in Java.
This is the famous interview question for the beginners as well as for experienced, So Let's see what it is all about.
Hashmap is very popular data structure and found useful for solving many problems due to O(1) time complexity for both get and put operation. Before getting into Hashmap internals, Please read Hashmap basics and Hashcode.
Internal working of Get and Put operation.
Hashmap store objects in key-value pair in a table.
1. Objects are stored by method hashmap.put(key, value) and
2. Objects are retrieved by calling hashmap.get(key) method.
For detail explanation on hashmap get and put API, Please read this post How Hashmap put and get API works.
Hashmap works on principle of hashing and internally uses hashcode as a base, for storing key-value pair.
With the help of hashcode, Hashmap stores objects and retrieves it in constant time O(1).
Lets recap "Employee Letter Box" example, we saw in last post on Hashcode.
1. Objects are stored by method hashmap.put(key, value) and
2. Objects are retrieved by calling hashmap.get(key) method.
For detail explanation on hashmap get and put API, Please read this post How Hashmap put and get API works.
Put Operation
Hashmap works on principle of hashing and internally uses hashcode as a base, for storing key-value pair.
With the help of hashcode, Hashmap stores objects and retrieves it in constant time O(1).
Lets recap "Employee Letter Box" example, we saw in last post on Hashcode.











