Video Series | Java Interview Questions
Hashmap
HashMap<String, String> map = new HashMap<>();
What internally happens when,
JVM creates new HashMap
only 16 buckets..??
So what if I want to put more than 16 items..??
Load Factor
CONCEPT
If HashMap reaches more than 75% of it’s capacity then it doubles the existing capacity
75%
Before diving into the internals, let’s see what exactly inside a bucket.!!
Linked List
Start
item-1
P
Let’s see what happens when we ADD nodes to Linked List
item-2
P
item-3
P
Now we know
How exactly buckets of HashMap look like
16
map.put( “key” , “value” );
Let’s see what happens when we put() items to HashMap
map.put( “FB” , ”A” );
“FB” => String Object
KEY
VALUE
Step-1: find hashcode
“FB”.hashCode()
Step-2: find bucket index
hashcode & (length-1)
= 2236 & (16 - 1)
hashcode = 2236
bucket index = 12
Example-1
map.put( “FB” , ”A” );
bucket index = 12
0.
1.
2.
12.
15.
..
..
..
..
[ “FB” , “A” ]
[ “FB” , “A” ]
P
1st node Linked List
Example-1
Let’s put one more item to the HashMap
map.put( “LD” , ”B” );
“LD” => String Object
Step-1: find hashcode
“LD”.hashCode()
Step-2: find bucket index
hashcode & (length-1)
2424 & (16 - 1)
hashcode = 2424
bucket index = 8
Example-2
map.put( “LD” , ”B” );
bucket index = 8
0.
8.
..
..
..
..
[ “LD” , “B” ]
[ “LD” , “B” ]
P
1st nodes in
different buckets
12.
[ “FB” , “A” ]
P
..
..
Example-2
Let’s put one more item to the HashMap
hash-collision
map.put( “Ea” , ”C” );
“Ea” => String Object
Step-1: find hashcode
“Ea”.hashCode()
Step-2: find bucket index
hashcode & (length-1)
2236 & (16 - 1)
hashcode = 2236
bucket index = 12
Example-2
map.put( “Ea” , ”C” );
bucket index = 12
0.
8.
..
..
[ “Ea” , “B” ]
[ “LD” , “B” ]
P
1st nodes in
different buckets
12.
[ “FB” , “A” ]
P
..
..
Example-3
Hash Collision
map.put( “Ea” , ”C” );
bucket index = 12
0.
8.
..
..
[ “Ea” , “B” ]
12
[ “FB” , “A” ]
P
Example-3
Check if two objects are equal?
“Ea”.equals(“FB”)
[ “Ea” , “C” ]
P
Summary
map.put( “key” , “value” )
Find hashCode() of the key:
“key”.hashCode()
Find bucket index using hashcode:
Hashcode & ( length - 1 )
Hash Collision
Simply add to Linked List as first node
No
Yes
Key already present??
“key”.equals(“existing-key”)
No
Yes
Add to Linked List by replacing existing equal node
Add to
Linked List as next node
Searching in Linked List
Start
item-1
P
item-2
P
item-3
P
item-4
P
How to find node with item-3 ?
Item-3 == item-1
Item-3 == item-2
Item-3 == item-3
item-3
Return node with item 3
get() method of HashMap
map.get ( “Ea” );
0.
1.
12.
15.
..
..
..
..
[ “FB” , “A” ]
P
8.
[ “Ea” , “C” ]
P
Step 1: HashCode of Ea | 2236
Step 2: Find bucket index | 12
Go to bucket index 12
Get the [key-value] pair
..
..
..
[ “Ea” , “C” ]
HashMap<String, String> map = new HashMap<>( );
map.put ( “AaAaAa” , “v1” );
map.put ( “AaAaBB” , “v2” );
map.put ( “AaBBAa” , “v3” );
map.put ( “AaBBBB” , “v4” );
map.put ( “BBAaAa” , “v5” );
map.put ( “BBAaBB” , “v6” );
map.put ( “BBBBAa” , “v7” );
map.put ( “AaBBBB” , “v8” );
map.put ( “BBBBBB” , “v9” );
=> 1952508096
=> 1952508096
=> 1952508096
=> 1952508096
=> 1952508096
=> 1952508096
=> 1952508096
=> 1952508096
=> 1952508096
0.
1.
2.
12.
15.
..
..
..
..
AaAaAa
P
AaAaBB
P
AaBBAa
P
AaBBBB
P
BBAaAa
P
BBAaBB
P
..
..
..
Performance Degradation
0.
12.
15.
..
4
P
2
P
7
P
6
P
5
P
..
3
P
1
P
Uses compareTo() method.
compareTo() method is use to check the order of items
It’s a red-black tree.
binary search tree
Self balancing
TREEIFY_THRESHOLD
NEXT VIDEO
More Practical Interview Questions on
HashMap
equal() + HashCode() contract
Overriding equals() and HashCode() methods
Thank you !!
References and attributions:
Icons made by <a href="https://www.flaticon.com/authors/freepik" title="Freepik">Freepik</a> from <a href="https://www.flaticon.com/" title="Flaticon"> www.flaticon.com</a>
Icons made by <a href="https://www.flaticon.com/authors/freepik" title="Freepik">Freepik</a> from <a href="https://www.flaticon.com/" title="Flaticon"> www.flaticon.com</a>
Icons made by <a href="https://www.flaticon.com/authors/wanicon" title="wanicon">wanicon</a> from <a href="https://www.flaticon.com/" title="Flaticon"> www.flaticon.com</a>
Icons made by <a href="https://www.flaticon.com/authors/freepik" title="Freepik">Freepik</a> from <a href="https://www.flaticon.com/" title="Flaticon"> www.flaticon.com</a>
Icons made by <a href="https://www.flaticon.com/authors/iconixar" title="iconixar">iconixar</a> from <a href="https://www.flaticon.com/" title="Flaticon"> www.flaticon.com</a>