Java / Concurrent collections
Difference between synchronizedMap and ConcurrentHashMap in Java.
The synchronizedMap(HashMap) locks the entire map while ConcurrentHashMap synchronizes or locks on the certain portion of the Map . To optimize the performance of ConcurrentHashMap , Map is divided into different Segments.
ConcurrentHashMap shows good performance than synchronized version of HashMap.
When you read from a ConcurrentHashMap using get(), there are no locks, contrary to the Synchronized HashMap for which all operations are simply synchronized.
ConcurrentHashMap can guarantee that there is no ConcurrentModificationException thrown while one thread is updating the map and another thread is traversing the iterator obtained from the map. However, Collections.synchronizedMap() is not guaranteed on this.
ConcurrentHashMap will not preserve the order of elements in the Map passed in. It is similar to HashMap when storing data. There is no guarantee that the element order is preserved. While Collections.synchronizedMap() will preserve the elements order of the Map passed in.
More Related questions...