Q1: What is HashMap in Java?
Answer:
HashMap is a class in the java.util package used to store key-value pairs.
- Not thread-safe
- Allows one null key and multiple null values
- Faster in single-threaded environments
Q2: What is ConcurrentHashMap?
Answer:
ConcurrentHashMap is a thread-safe implementation of Map from java.util.concurrent.
- Designed for multi-threaded applications
- Does NOT allow null key or value
- Provides high performance with concurrency
Q3: What is the main difference between HashMap and ConcurrentHashMap?
Answer:
The main difference is thread safety:
HashMap→ Not thread-safeConcurrentHashMap→ Thread-safe
Q4: Why is HashMap not thread-safe?
Answer:
Because it does not use synchronization.
Multiple threads can modify it simultaneously, leading to:
- Data inconsistency
- Infinite loops (in older Java versions during resizing)
Q5: How does ConcurrentHashMap achieve thread safety?
Answer:
- Java 7 → Uses segment-level locking
- Java 8+ → Uses CAS (Compare-And-Swap) and synchronized blocks
This ensures that multiple threads can work concurrently without locking the entire map.
Q6: Does ConcurrentHashMap allow null keys and values?
Answer:
No, it does not allow null keys or values.
Reason:
To avoid ambiguity in concurrent environments (e.g., null meaning absence or value).
Q7: Does HashMap allow null keys and values?
Answer:
Yes
- One null key allowed
- Multiple null values allowed
Q8: What is fail-fast and fail-safe iterator?
Answer:
- Fail-Fast (HashMap)
- Throws
ConcurrentModificationExceptionif modified during iteration
- Throws
- Fail-Safe (ConcurrentHashMap)
- Does not throw exception
- Iterates over a snapshot
Q9: Which is faster: HashMap or ConcurrentHashMap?
Answer:
HashMap→ Faster in single-threaded applicationsConcurrentHashMap→ Optimized for multi-threaded performance
Q10: What happens if multiple threads use HashMap?
Answer:
- Data corruption may occur
- Unexpected behavior
- Not safe for concurrent use
Q11: What is the locking mechanism in ConcurrentHashMap?
Answer:
- Uses fine-grained locking
- Locks only a portion of the map instead of the whole map
Q12: Can we make HashMap thread-safe?
Answer:
Yes, using:
Map map = Collections.synchronizedMap(new HashMap<>());
But this locks the entire map → reduces performance.
Q13: When should we use HashMap?
Answer:
- Single-threaded applications
- No concurrent modification
- High performance required
Q14: When should we use ConcurrentHashMap?
Answer:
- Multi-threaded applications
- Thread safety required
- High scalability needed
Q15: What is a real-world example?
Answer:
Example: Cache system
- Using HashMap → Not safe
- Using ConcurrentHashMap → Safe and efficient
Final Interview Summary
HashMap→ Not thread-safe, allows nullConcurrentHashMap→ Thread-safe, no null allowed- Iterators:
- HashMap → Fail-fast
- ConcurrentHashMap → Fail-safe
Pro Tip (For Interview)
If interviewer asks:
“Which one will you use in production?”
Answer:
“For multi-threaded environments, I prefer ConcurrentHashMap because it provides thread safety with better performance using fine-grained locking instead of synchronizing the entire map.”