...

/

HashMap: Internal Implementation

HashMap: Internal Implementation

Let's look at how HashMap works internally.

How HashMap works internally is the most asked interview question in Java interviews. And the reason is that it is difficult to understand the inner workings of HashMap. In this lesson, we will try to understand every aspect of HashMap.

The basic principle used by a HashMap to store elements is Hashing. Hashing is a way to assign a unique code for any variable or object after applying any formula to its properties. The unique code is called HashCode.

Some of the properties of HashCode are:

  1. If two objects are equal, then they should have the same hashcode.
  2. If two objects have the same hashcode, then it is not necessary for them to be equal.

Creating a HashMap

We already know that the HashMap stores key-value pairs. HashMap has a nested static class called Node as shown below.

static class Node<K,V>
...