Skip to content
-
Subscribe to our newsletter & never miss our best posts. Subscribe Now!
Cracking My Interview - Your Interview Partner Cracking My Interview - Your Interview Partner

Cracking My Interview - Your Interview Partner

Cracking My Interview - Your Interview Partner Cracking My Interview - Your Interview Partner

Cracking My Interview - Your Interview Partner

  • Java
  • Designs
  • Data Structure
  • Micro Services
  • Spring Boot
  • Machine Learning
  • Big Data
  • Basics
  • Computer Basics
  • Cyber Security
  • Java
  • Designs
  • Data Structure
  • Micro Services
  • Spring Boot
  • Machine Learning
  • Big Data
  • Basics
  • Computer Basics
  • Cyber Security
Java

How Hashmap stores values ? What is hash collision ? How store values in case of hash collision in Java ?

By SND
June 18, 2026 3 Min Read
0

1. Internal Structure

A HashMap internally maintains an array of buckets.

HashMap

Index Bucket
----- ------
0 null
1 null
2 null
3 null
4 null
5 null
6 null
7 null
...
15 null

Suppose capacity = 16.


2. Storing a Value

map.put("John", 100);

Step 1: Calculate hash

hash = hash("John");

Java actually does:

hash = key.hashCode() ^ (key.hashCode() >>> 16);

Let’s assume:

hash = 53

Binary:

53 = 110101

Step 2: Find Bucket Index

HashMap uses:

index = (arraySize - 1) & hash;

For capacity 16:

n = 16
n - 1 = 15

Binary:

hash = 110101
mask = 001111
----------------
index=000101

Result:

index = 5

Step 3: Store Entry

Bucket 5

[John,100]

Current Map:

0  → null
1 → null
2 → null
3 → null
4 → null
5 → [John,100]
6 → null
...
15 → null

3. What is a Hash Collision?

Now insert:

map.put("Alex", 500);

Assume:

hash("Alex") = 37

Calculate bucket:

37 & 15 = 5

Again Bucket 5!

Two different keys map to the same bucket.

This is called a hash collision.


4. How Collision is Stored

Java stores entries as a linked list (or tree later).

Bucket 5

┌───────────┐
│John,100 │
└─────┬─────┘
│
▼
┌───────────┐
│Alex,500 │
└───────────┘

So internally:

Bucket5
↓
John → Alex

5. Add Another Collision

map.put("Sara", 300);

Assume:

hash("Sara") & 15 = 5

Again Bucket 5.

Bucket5

John
↓
Alex
↓
Sara

6. Retrieving a Value

Now:

map.get("Alex");

Step 1

Compute hash

hash("Alex")

Assume:

37

Step 2

Find bucket

37 & 15 = 5

Go directly to Bucket 5.

Bucket5

John
↓
Alex
↓
Sara

Step 3

Compare Keys Using equals()

Check first node:

"Alex".equals("John")

Result:

false

Move next.


Check second node:

"Alex".equals("Alex")

Result:

true

Return:

500

Complete Retrieval Flow

get("Alex")

Key
│
▼
hashCode()
│
▼
hash
│
▼
(n-1) & hash
│
▼
Bucket 5
│
▼

John
│
equals?
│
No
▼

Alex
│
equals?
│
Yes
▼

Return 500

Why Use (n-1) & hash?

Instead of:

hash % 16

Java uses:

(hash & 15)

because:

Bitwise AND << much faster than %

This works because HashMap capacity is always:

16
32
64
128
256
...

(power of 2)


Java 8 Improvement

Suppose many collisions happen:

Bucket5

John
↓
Alex
↓
Sara
↓
Mike
↓
Tom
↓
Bob
↓
Jack
↓
David
↓
Emma

Searching becomes slow.

Before Java 8:

Linked List

Time Complexity = O(n)

Java 8+ converts long chains into a Red-Black Tree (when bucket size exceeds 8 and table is sufficiently large).

            John
/ \
Alex Sara
/ \
Bob Tom

Lookup becomes:

O(log n)

instead of

O(n)

End-to-End HashMap Flow

put("John",100)

Key
│
▼
hashCode()
│
▼
hash = h ^ (h >>> 16)
│
▼
index = (n-1) & hash
│
▼
Bucket 5
│
▼
Store Entry


get("John")

Key
│
▼
hashCode()
│
▼
hash = h ^ (h >>> 16)
│
▼
index = (n-1) & hash
│
▼
Bucket 5
│
▼
Traverse nodes
using equals()
│
▼
Return Value

Interview Summary

HashMap stores key-value pairs in an array of buckets. It computes a hash from the key and determines the bucket index using (tableSize - 1) & hash. If multiple keys map to the same bucket, a hash collision occurs. Java handles collisions using a linked list and, since Java 8, converts long collision chains into a Red-Black Tree. During retrieval, HashMap recalculates the bucket index, traverses the bucket, compares keys using equals(), and returns the matching value. This gives average-case O(1) performance for put and get operations.

Why different key can have same hashcode?

In Java both can have same hashcode

“FB”.hashCode(); // 2236
“Ea”.hashCode(); // 2236

because Hashcode method can return -> Only 2^32 possible int values.

Why Only 2^32 possible int values?

Because hashCode() method retrun int and int is 32 bit in Java.

Author

SND

Technology leader with 24 years of experience designing and delivering large-scale enterprise applications across multiple industries. Expertise in Java, Spring ecosystem, cloud-native architectures, and distributed systems. Strong background in Big Data, machine learning, and building scalable, high-performance platforms. Extensive experience with open-source technologies, databases, microservices, and modern application modernization initiatives. Proven track record of leading architecture, engineering, and digital transformation programs from concept to production.

Follow Me
Other Articles
Previous

Hardware vs Software

Next

Which design patterns are most heavily used in Spring?

No Comment! Be the first one.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • From Physical Servers to Kubernetes: The Complete Evolution of Application Networking (Explained with real example – TravelCity)
  • Retrieval-Augmented Generation (RAG): Architecture, Pipeline, and Enterprise Implementation
  • Building Enterprise AI Applications with RAG and LangChain
  • From ChatGPT to AI Agents to MCP: Understanding the Evolution of Enterprise AI
  • Service Discovery in Microservices: From Eureka to Kubernetes

Recent Comments

  1. Tom on Web Application Architecture in AWS (Amazon)
  2. A WordPress Commenter on DESIGN A LOG AGGREGATION SYSTEM

Archives

  • July 2026
  • June 2026

Categories

  • Basics
  • Computer Basics
  • Cyber Security
  • Data Structure
  • Designs
  • Java
  • Machine Learning
  • Micro Services
  • Spring Boot
  • AI ML LLM Agents
  • Java SpringBoot REST
  • Design Problems
  • Data Structure
Contact us

contact@crackingmyinterview.com

  • YouTube
  • Facebook
Copyright 2026 — Cracking My Interview - Your Interview Partner. All rights reserved. Blogsy WordPress Theme