Wednesday, June 24, 2015

HashMap v/s ConcurrentHashMap

Concurrence


Going a step further interviewer now delves into the multi-threading regime. One of the first questions that is asked at mid-level (2-4 yrs) is difference between HashMap and ConcurrentHashMap. 

This is check of knowledge of basic usage of the Candidate, since if you have worked on any Java application, you would have come across HashMap/ConcurrentHashMap for sure.


S No.
HashMap
ConcurrentHashMap
1
Not Thread safe
Thread safe.
2
synchronized(map) makes HashMap synchronized
Already thread safe
3
synchronized(map) locks entire map. Reduces Performance
Takes lock only on a portion of the Map and not the whole Object. Faster Performance
4
One null key is allowed
Null key is not allowed
5
Better performance in Single threaded Environment
Not Got performance in Single threaded Environment

No comments:

Post a Comment