DEV Community

Cover image for How Does HashMap Work Internally in Java?
Gowtham Kalyan
Gowtham Kalyan

Posted on

How Does HashMap Work Internally in Java?

1. Introduction

HashMap is one of the most important data structures in Java used to store key-value pairs.

It provides constant time complexity O(1) for put() and get() operations (on average) by using a technique called hashing.


2. Basic Working Concept

Explanation

HashMap internally uses:

  • Array (Bucket array)
  • Hashing (hashCode)
  • Linked List / Tree (for collision handling)

Each key-value pair is stored in a structure called a Node.


3. Internal Structure

A simplified structure of HashMap:

static class Node<K, V> {
    final int hash;
    final K key;
    V value;
    Node<K, V> next;
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • hash → hash value of the key
  • key → actual key
  • value → stored value
  • next → reference to next node (for collision handling)

4. How put() Works

Example

import java.util.HashMap;

public class PutExample {
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1, "Java");
        map.put(2, "Python");
    }
}
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Explanation

  1. Hash Calculation
  • hashCode() is called on the key.
  • Example: key 1 → hash value generated.
  1. Index Calculation
  • Index = (n - 1) & hash
  • Determines position in the bucket array.
  1. Store Entry
  • If bucket is empty → insert directly.
  • If not → handle collision.

5. How get() Works

Example

import java.util.HashMap;

public class GetExample {
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1, "Java");

        System.out.println(map.get(1));
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  1. Calculate hash of key
  2. Find bucket index
  3. Traverse nodes (if multiple)
  4. Match using equals()
  5. Return value

6. Collision Handling

Explanation

Collision occurs when two keys map to the same index.

Before Java 8

  • Stored using Linked List

After Java 8

  • If bucket size > 8 → converted to Balanced Tree (Red-Black Tree)
  • Improves performance from O(n) → O(log n)

7. Example of Collision

import java.util.HashMap;

public class CollisionExample {
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();

        map.put(1, "A");
        map.put(17, "B"); // Might collide depending on capacity

        System.out.println(map);
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • Both keys may go to same bucket.
  • Stored as:

    • Linked List OR
    • Tree (if threshold exceeded)

8. Important Concepts

Load Factor

  • Default: 0.75
  • Determines when to resize the HashMap

Resizing (Rehashing)

  • When capacity exceeds threshold:

    • New array is created (double size)
    • All elements are rehashed

9. Performance

Operation Average Time Worst Case
put() O(1) O(log n)
get() O(1) O(log n)

10. Key Points

  • Uses hashCode() and equals()
  • Allows one null key and multiple null values
  • Not thread-safe
  • Faster retrieval due to hashing

11. Real-Time Use Case

  • Caching data
  • Storing configurations
  • Database result mapping
  • Counting frequency of elements

Java Full Stack Developer Roadmap

To master concepts like HashMap, Collections, and System Design:

👉 https://www.ashokit.in/java-full-stack-developer-roadmap


Promotional Content

Want to deeply understand Java Collections and crack interviews?

Top Core JAVA Online Training in 2026

Top comments (0)