HashMap in Java Explained β Internal Working, Evolution & Real Insights
π₯ Table of Contents
- Introduction to HashMap
- Why Do We Need HashMap
- Internal Working of HashMap
- Java 8 Enhancements in HashMap
- Practical Implementation in Java
- Summary
- Interview Questions
Introduction to HashMap
HashMap is one of the most commonly used data structures in Java from the java.util package.
π It stores data in key-value pairs
π It allows null key (only one) and multiple null values
π It is NOT thread-safe
π It provides constant time performance O(1) for basic operations (get/put)
Example:
import java.util.HashMap;
public class Example {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "Python");
map.put(3, "C++");
System.out.println(map.get(2)); // Python
}
}
Why Do We Need HashMap
Imagine you need:
π Fast lookup by key
π Unique keys
π Efficient insertions
HashMap solves all of these efficiently.
Without HashMap:
- Searching in a list β O(n)
With HashMap:
- Searching β O(1) average case β
π‘ Real-world usage:
- Caching
- Database indexing
- Counting frequency of elements
Internal Working of HashMap
Letβs break this down step by step.
Hashing Mechanism
π When you insert a key:
map.put("apple", 10);
Java does:
π Calls hashCode() on key
π Converts it into a hash
π Maps it to an index in array
Formula:
π index = hash & (n – 1)
Where n = capacity of array
Internal Data Structure
HashMap internally uses:
π Array of Nodes (buckets)
Each bucket contains:
static class Node<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;
}
π Each bucket acts like a LinkedList (before Java 8)
Collision Handling
π₯ Collision happens when:
Two keys produce same index
Example:
map.put("FB", 1);
map.put("Ea", 2);
These two strings produce same hashCode!
π Then they are stored in same bucket using LinkedList
Structure:
Bucket β [Node1] β [Node2] β [Node3]
β οΈ Problem:
Too many collisions β performance becomes O(n)
Retrieval Process
map.get("apple");
Steps:
π Calculate hash
π Find bucket index
π Traverse linked list
π Compare keys using equals()
π Return value if match found
Resizing and Rehashing
π₯ Default capacity = 16
π₯ Load factor = 0.75
When size exceeds:
π capacity * loadFactor (16 * 0.75 = 12)
Then:
π Array size doubles (16 β 32)
π All entries are rehashed
β οΈ Rehashing is expensive operation
Java 8 Enhancements in HashMap
Java 8 brought a major optimization π₯
Treeification (LinkedList β Red-Black Tree)
When number of nodes in a bucket exceeds 8:
π LinkedList β Red-Black Tree
Condition:
π bucket size β₯ 8 AND capacity β₯ 64
Performance Improvement
π LinkedList search = O(n)
π Tree search = O(log n) β
This significantly improves worst-case performance.
Untreeification
If nodes drop below 6:
π Tree β LinkedList again
Improved Hashing Technique
Java 8 improves hash distribution:
hash = key.hashCode() ^ (hash >>> 16);
π This reduces collisions
Practical Implementation in Java
Letβs simulate collision and understand behavior.
import java.util.HashMap;
class Key {
String value;
Key(String value) {
this.value = value;
}
@Override
public int hashCode() {
return 1; // Force collision
}
@Override
public boolean equals(Object obj) {
return this.value.equals(((Key) obj).value);
}
}
public class HashMapInternalDemo {
public static void main(String[] args) {
HashMap<Key, String> map = new HashMap<>();
map.put(new Key("A"), "Apple");
map.put(new Key("B"), "Ball");
map.put(new Key("C"), "Cat");
System.out.println(map);
}
}
π All keys go into same bucket due to same hashCode
π‘ This helps visualize collision handling
Important Internal Methods
Some key internal methods:
π putVal() β handles insertion
π resize() β handles rehashing
π treeifyBin() β converts list to tree
π getNode() β retrieves value
Summary
β
HashMap uses array + hashing
β
Collisions handled using LinkedList (Java 7)
β
Java 8 uses Red-Black Tree for optimization
β
Average complexity β O(1)
β
Worst case improved from O(n) β O(log n)
π‘ Key takeaway:
Good hashCode() implementation is critical for performance
Interview Questions
- What is HashMap and how does it work internally?
π It uses hashing and array of buckets to store key-value pairs. - What is collision in HashMap?
π When multiple keys map to same bucket index. - How does Java 8 improve HashMap performance?
π Converts LinkedList into Red-Black Tree after threshold. - What is load factor?
π It defines when resizing should happen (default 0.75). - Why hashCode() and equals() both are required?
π hashCode() finds bucket, equals() finds exact key. - What is treeification threshold?
π 8 nodes in a bucket. - Is HashMap thread-safe?
π No. - What happens during rehashing?
π Capacity doubles and all entries are redistributed.
