HashMap Part-1

The most important interview question of java is Collection and In Collection the most important topic is HashMap. Which is asked in every interview. If you fail to give the answer to the explanation of HashMap, then definitely you will get rejected.

In this article, we will give you an idea of how HashMap works and What are possible interview question that has been asked by the interviewer.

  1. A real-life example of Hashmap – Where it can fit in a real-life example (part 1)
  2. HashMap structure based on the example (Part-2)
  3. How HashMap works (Part-3)
  4. Object as key ()
  5. What if method not overloaded Case
  6. Fail Fast HashMap

Real-Life Example

Consider a scenario where you have a list of Person names with its contact details. We need to store all names and their contact numbers without using any technical gadget like a mobile or something like that.

Example:

NameContact Number
Tom Cruise
Johnny Depp
Brad Pitt
Angelina Jolie
Arnold Schwarzenegger
Sylvester Stallone
Christian Bale
Hugh Jackman
.
.
.
XXXX12345565
XXXX12345565
XXXX12345565
XXXX12345565
XXXX12345565
XXXX12345565
XXXX12345565
XXXX12345565
.
.
.

So as a simple solution what you will do, you will write Name and Contact details pair in a normal notebook. The list is very huge and names are in random alphabetic order.

The list is very huge and when you need the contact details based on name you will search for that particular name in a notebook one by one. In technical terms, it is called Linear search, and its time complexity will be O(n).

How we can make it faster??? That will save search time.

Solution:

As you can see in the above image, Now we have an alphabetic indexing notebook that will save our search time. Below are the writing and searching steps in an alphabetically indexed notebook.

Writing steps:

  1. Get Name – Contact Pair
  2. Identify the First alphabetic character of Name
  3. Jump to that alphabetic page
  4. Write the Name – Contact details

Searching steps :

  1. Get the Name
  2. Identify the First alphabetic character of Name
  3. Jump to that alphabetic page
  4. Search for that name in that page only and get the contact details

This indexing based writing and a searching operation called the Hashing Mechanism.

HashMap Internal Structure

HashMap Structure

HashMap internally works based on bucket indexing. It is similar to the phonebook example that we have covered.

Lets first cover why we are using HashMap. HashMap is used to store a Key-Value pair. The provided key should be unique then only we can store our value. If key is already present int the HashMap the value will overwrite the existing data. While retrieving from the HashMap we pass key as argument to get that particular value from the HashMap. HashMap will identify Bucket number and iterate that list and return that value.

Each Bucket contains its own singly link list. Whenever put() or get() operation happens it happens on this list. So how this put() or get() operation HashMap manage internally we have explained it in our next part.

This article is for to just cover the real-time example of HashMap. Let us know if you have any doubt we will be happy to solve that.

Happy Reading 🙂

Rahul Chudasama
Leave a Reply