1. Introduction
Both HashMap and ConcurrentHashMap are used to store key-value pairs, but they differ mainly in:
- Thread safety
- Performance in multi-threaded environments
- Internal working mechanism
2. HashMap
Explanation
-
HashMapis not thread-safe. -
Multiple threads accessing it simultaneously may lead to:
- Data inconsistency
- Infinite loops (in older Java versions during resizing)
It is best suited for single-threaded environments.
Example
import java.util.HashMap;
public class HashMapExample {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "Python");
System.out.println(map);
}
}
Explanation of Code
- Simple key-value storage
- No synchronization → faster but unsafe in multi-threading
3. ConcurrentHashMap
Explanation
-
ConcurrentHashMapis thread-safe. - Designed for high concurrency.
-
Uses advanced techniques like:
- Segment locking (Java 7)
- CAS (Compare-And-Swap) & synchronized blocks (Java 8+)
Allows multiple threads to read/write simultaneously.
Example
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentMapExample {
public static void main(String[] args) {
ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>();
map.put(1, "Java");
map.put(2, "Python");
System.out.println(map);
}
}
Explanation of Code
- Thread-safe operations without locking entire map
- Better performance than Hashtable in multi-threading
4. Key Differences Table
| Feature | HashMap | ConcurrentHashMap |
|---|---|---|
| Thread Safety | Not thread-safe | Thread-safe |
| Performance | Faster (single thread) | Optimized for multi-thread |
| Locking Mechanism | No locking | Partial locking / CAS |
| Null Keys/Values | Allows 1 null key | Does NOT allow null key/value |
| Fail-fast / Fail-safe | Fail-fast iterator | Fail-safe iterator |
| Use Case | Single-threaded apps | Multi-threaded apps |
5. Internal Working Difference
HashMap
- Uses array + linked list / tree
- No synchronization
- Entire structure is unsafe in concurrent access
ConcurrentHashMap
- Divides map into segments (Java 7) OR
- Uses bucket-level locking (Java 8+)
- Only a portion of the map is locked during updates
6. Multi-threading Example
HashMap (Problem)
import java.util.HashMap;
public class HashMapThreadIssue {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
Runnable task = () -> {
for (int i = 0; i < 1000; i++) {
map.put(i, "Value");
}
};
new Thread(task).start();
new Thread(task).start();
}
}
Explanation
- Multiple threads modify map simultaneously
- May lead to inconsistent data
ConcurrentHashMap (Solution)
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapSafe {
public static void main(String[] args) {
ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>();
Runnable task = () -> {
for (int i = 0; i < 1000; i++) {
map.put(i, "Value");
}
};
new Thread(task).start();
new Thread(task).start();
}
}
Explanation
- Safe concurrent updates
- No data corruption
7. When to Use What?
-
Use HashMap when:
- Application is single-threaded
- No concurrent modifications
-
Use ConcurrentHashMap when:
- Multiple threads access/modify data
- High performance is required
8. Summary
- HashMap → Fast but not thread-safe
- ConcurrentHashMap → Thread-safe with high performance
- Choose based on concurrency needs
Java Full Stack Developer Roadmap
To master collections and multithreading concepts:
👉 https://www.ashokit.in/java-full-stack-developer-roadmap
Promotional Content
Want to master Java Collections and Multithreading concepts like HashMap and ConcurrentHashMap?
Top comments (0)