1 of 22

Video Series | Java Interview Questions

  1. Internal Working of

Hashmap

2 of 22

  • Internal memory structure of Hashmap.

  • How put() method internally works.

  • What exactly is Hash Collision

  • Use of equals() and hashCode() in HashMap

  • Java 8 enhancement to HashMap

3 of 22

  1. Internal structure or working of Hashmap.

HashMap<String, String> map = new HashMap<>();

What internally happens when,

JVM creates new HashMap

4 of 22

  • Internal structure or working of 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%

5 of 22

  • Internal structure or working of Hashmap.

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

6 of 22

  • Internal structure or working of Hashmap.

Now we know

How exactly buckets of HashMap look like

16

map.put( “key” , “value” );

7 of 22

  • Internal structure or working of Hashmap.

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

  1. hashcode of FB = 2236 | index 12

8 of 22

  • Internal structure or working of Hashmap.

map.put( “FB” , ”A” );

bucket index = 12

0.

1.

2.

12.

15.

..

..

..

..

[ “FB” , “A” ]

[ “FB” , “A” ]

P

1st node Linked List

Example-1

  • hashcode of FB = 2236 | index 12

9 of 22

  • Internal structure or working of Hashmap.

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

  • hashcode of FB = 2236 | index 12

10 of 22

  • Internal structure or working of Hashmap.

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

  • hashcode of FB = 2236 | index 12
  • hashcode of LD = 2424 | index 8

11 of 22

  • Internal structure or working of Hashmap.

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

  • hashcode of FB = 2236 | index 12
  • hashcode of LD = 2424 | index 8
  • hashcode of Ea = 2236 | index 12

12 of 22

  • Internal structure or working of Hashmap.

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

  • hashcode of FB = 2236 | index 12
  • hashcode of LD = 2424 | index 8
  • hashcode of Ea = 2236 | index 12

Hash Collision

13 of 22

  • Internal structure or working of Hashmap.

map.put( “Ea” , ”C” );

bucket index = 12

0.

8.

..

..

[ “Ea” , “B” ]

12

[ “FB” , “A” ]

P

Example-3

  • hashcode of FB = 2236 | index 12
  • hashcode of LD = 2424 | index 8
  • hashcode of Ea = 2236 | index 12

Check if two objects are equal?

“Ea”.equals(“FB”)

[ “Ea” , “C” ]

P

14 of 22

  • Internal structure or working of Hashmap.

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

15 of 22

  • Internal structure or working of Hashmap.

  • How put() method internally works.

  • What exactly is Hash Collision

  • Use of equals() and hashCode() in HashMap

  • Java 8 enhancement to HashMap

16 of 22

  • Java 8 enhancement to HashMap

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

17 of 22

  • Java 8 enhancement to HashMap

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” ]

18 of 22

  • Java 8 enhancement to HashMap

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

19 of 22

  • Java 8 enhancement to HashMap

0.

1.

2.

12.

15.

..

..

..

..

AaAaAa

P

AaAaBB

P

AaBBAa

P

AaBBBB

P

BBAaAa

P

BBAaBB

P

..

..

..

Performance Degradation

20 of 22

  • Java 8 enhancement to HashMap

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

21 of 22

NEXT VIDEO

More Practical Interview Questions on

HashMap

equal() + HashCode() contract

Overriding equals() and HashCode() methods

Thank you !!

22 of 22

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>