Friday 26 August 2022

How Hashmap works Internally?


HashMap<K,V> is a class in Java Collection Framework implementing Map<K,V> interface. HashMap fully supports all features specified in the Map interface, including optional features.
HashMap allows to store key and value pairs. Keys cannot be duplicated.

public class HashMap<K,V> extends AbstractMap<K,V>
                      implements Map<K,V>, Cloneable, Serializable
The characteristics of HashMap:
  • HashMap contains key and value pairs.
  • It can have one null key and multiple null values.
  • HashMap does not maintain keys order.
  • it works base on hashing technique. (See more explanation of this technique below)
Java designers have used hashing technique in HashMap class to store data and improve its performance. Now lets see how this technique is used in HashMap. We basically analyze what happens when we call these methods: HashMap.put(K,V), HashMap.get(K) and HashMap.remove(key).
HashMap<K,V> manages an array of Node<K,V> objects that will be replaced by another larger array if all of its elements has been assigned value.

static final int hash(Object key) {
    int h;
    return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
Node<K,V> class consists of 4 fields. next field is a reference to the next Node object. It may not be the next element in the array.

static class Node<K,V> implements Map.Entry<K,V> {
    int hashcode;
    K key;
    V value;
    Node<K,V> next;
HashMap ensures those Node(s) having the same hashcode will have consecutive references. This helps it quickly find all Node(s) with the same specified hashcode .
When calling HashMap.put(key,value) method, HashMap looks for Node with condition node.hashcode == hash(key). If no match is found, a new Node object is assigned to the array. (See picture below)
Otherwise, HashMap quickly identifies those consecutive Node(s) satisfying condition node.hashcode == hash(key) and significantly narrows the scope of Node(s) to be searched by key.
  • If the Node corresponding to the key is found, new value will be assigned to Node.value.
  • Otherwise, a new Node object is created and assigned to the array (See picture below).
When HashMap.get(key) method is called, HashMap quickly identifies consecutive Node(s) that satisfy the condition node.hashcode == hash(key), which significantly narrows the scope of the Node(s) need to be searched by key. Then, it searches Node by key and returns node.value if found, otherwise it returns null.


✍️AUTHORLinkedIn Profile


Learn (API-Microservice)Testing+ Selenium UI Automation-SDET with Self Paced Videos prepared by FAANG employee and LIVE Doubt Session 

SDET TRANING VIDEOS AVAILABLE with Live Doubt Session(course-1 below,API TRaining Videos With Class Notes and Coding Set) and (API+UI, both course-1 & 2 below) Check Training Page for Course Content or reach out @whatsapp +91-9619094122. 
This includes classnotes, 300+ interview questions, 3 projects, Java Coding question set for product companies along with career guidance from FAANG employees for Automation and SDET.

For more details whatsapp :


SeleniumWebdriver Automation Testing Interview Questions:

API Testing Interview Question Set:

DevOps Interview Q&A: 

Kubernetes Interview Question Set

Docker Interview Question Set

Linux Interview question Set

Automation Testing/SDET Framework Design

Java Related Interview Question Set

GIT Interview Question Set:

Coding Interview Question Set:

Mobile Testing Interview Question Set:

Python Interview Question Set for QAE - SDET - SDE:


No comments:

Post a Comment

All Time Popular Posts

Most Featured Post

Constructor in JAVA for QAE - SDET - SDE ?

  Constructor is a special member function, these are used to initialize the values of objects. When we create an object using the   new()  ...