HashMap Vs HashTable Vs SynchronizedMap Vs ConcurrentHashMap

Connect with

Oracle JavaThere are number of question in mind when we think about HashMap, Hashtable, SynchronizedMap and ConcurrentHashMap. All uses Map data structure to keep the data but one main difference is locking. Let us go one by one as follows.


  • HashMap is an implementation of Map data structure
  • for single threaded environment
  • Not thread-safe
  • can keep one null key and multiple null value.

2. HashTable

  • Hashtable is an implementation of Map data structure
  • This is a legacy class in which all methods are synchronized on Hashtable instances using synchronized keyword.
  • Thread-safe as it’s method are synchronized

3. SynchronizedMap

  • This is wrapper of Map, and can be initialize by using Collections.synchcronizedMap(new HashMap())
  • similar to Hashtable
  • The only difference between Hashtable and Synchronized Map is that later is not a legacy and you can wrap any Map to create it’s synchronized version by using Collections.synchronizedMap() method.

4. ConcurrentHashMap

  • ConcurrentHashMap implements Map data structure and also provide thread safety like Hashtable.
  • It works by dividing complete hashtable array in to segments or portions and allowing parallel access to those segments.
  • The locking is at a much finer granularity at a hashmap bucket level.
  • Use ConcurrentHashMap when you need very high concurrency in your application.
  • It is thread-safe without synchronizing the whole map.
  • Reads can happen very fast while write is done with a lock on segment level or bucket level.
  • There is no locking at the object level.
  • ConcurrentHashMap doesn’t throw a ConcurrentModificationException if one thread tries to modify it while another is iterating over it.
  • ConcurrentHashMap does not allow NULL values, so key can not be null in ConcurrentHashMap
  • ConcurrentHashMap doesn’t throw a ConcurrentModificationException if one thread tries to modify it, while another is iterating over it.

5. Difference between Hashtable, HashMap and concurrent HashMap

[table “” not found /]

6. Putting It Together

  • ConcurrentHashMap and CopyOnWriteArrayList implementations provide much higher concurrency while preserving thread safety, with some minor compromises in their promises to callers. ConcurrentHashMap and CopyOnWriteArrayList are not necessarily useful everywhere you might use HashMap or ArrayList, but are designed to optimize specific common situations. Many concurrent applications will benefit from their use.
  • The synchronized collections classes, Hashtable, and Vector, and the synchronized wrapper classes, Collections.synchronizedMap() and Collections.synchronizedList(), provide a basic conditionally thread-safe implementation of Map and List. However, several factors make them unsuitable for use in highly concurrent applications, e.g, their single collection-wide lock is an impediment to scalability and it often becomes necessary to lock a collection for a considerable time during iteration to prevent ConcurrentModificationException.
  • ConcurrentHashMap is specially designed for concurrent use i.e. more than one thread. By default it simultaneously allows 16 threads to read and write from Map without any external synchronization. It is also very scalable because of stripped locking technique used in the internal implementation of ConcurrentHashMap class. Unlike Hashtable and Synchronized Map, it never locks whole Map, instead, it divides the map into segments and locking is done on those. Though it performs better if a number of reader threads are greater than the number of writer threads.
  • ConcurrentHashMap uses a database shards logic(Segment[] segments) , i.e. divide the data into shards(segments) than puts locks on each shard (segment) instead of putting single lock for whole data(Map)

Suggestions are welcome always to improve this post.

Connect with

4 thoughts on “HashMap Vs HashTable Vs SynchronizedMap Vs ConcurrentHashMap

  1. Aw, this was an exceptionally nice post. Taking the time and actual effort to create a
    good article? but what can I say? I put things off a lot
    and never manage to get nearly anything done.

Leave a Reply

Your email address will not be published. Required fields are marked *