94

What is the difference between a ConcurrentHashMap and a Hashtable in Java?

Which is more efficient for threaded applications?

4
  • 2
    For a non-threaded application, use HashMap. Commented Sep 28, 2012 at 19:48
  • It is always recommended to use concurrenthashmap over hashtable. Please find the article on it ibm.com/developerworks/java/library/j-jtp07233/index.html Commented Sep 28, 2012 at 19:50
  • Also see stackoverflow.com/a/40878/632951 for more info. Commented Aug 20, 2014 at 15:06
  • @Keith Randall, There is no non-threaded application in java. Its either multithreaded or single threaded (only main thread). You are talking about single threaded application. Commented Aug 20, 2020 at 11:02

2 Answers 2

177

ConcurrentHashMap and Hashtable locking mechanism

  • Hashtable is belongs to the Collection framework; ConcurrentHashMap belongs to the Executor framework.
  • Hashtable uses single lock for whole data. ConcurrentHashMap uses multiple locks on segment level (16 by default) instead of object level i.e. whole Map.
  • ConcurrentHashMap locking is applied only for updates. In case of retrievals, it allows full concurrency, retrievals reflect the results of the most recently completed update operations. So reads can happen very fast while writes are done with a lock.
  • ConcurrentHashMap doesn't throw a ConcurrentModificationException if one thread tries to modify it while another is iterating over it and does not allow null values.
  • ConcurrentHashMap returns Iterator, which fails-safe (i.e. iterator will make a copy of the internal data structure) on concurrent modification.
  • ConcurrentHashMap uses a database shards logic (Segment<K, V>[] segments) is known as Concurrency-Level, i.e. divides the data into shards(segments) then puts locks on each shard (segment) instead of putting a single lock for whole data (Map). The default value is 16.

To understand the ConcurrentHashMap more technically please look at this link

The following analogy helps you get understand the concept only(not logic)

  • Assume Hashtable and ConcurrentHashMap are two types of Homes.
  • Hashtable locks home's main door.
  • ConcurrentHashMap locks specific room door instead of main door.

Which is more efficient for threaded applications?

ConcurrentHashMap is more efficient for threaded applications.

Sign up to request clarification or add additional context in comments.

3 Comments

Home example won over all the theory :)
Does this mean ConcurrentHashMap is better than HashTable? In what case HashTable is better than the other one?
@Tiina there is never a case when Hashtable is the preferred class. If your process needs to be thread-safe, use ConcurrentHashMap. If not, use java.util.HashMap.
125

ConcurrentHashMap uses multiple buckets to store data. This avoids read locks and greatly improves performance over a HashTable. Both are thread safe, but there are obvious performance wins with ConcurrentHashMap.

When you read from a ConcurrentHashMap using get(), there are no locks, contrary to the HashTable for which all operations are simply synchronized. HashTable was released in old versions of Java whereas ConcurrentHashMap is a java 5+ thing.

HashMap is the best thing to use in a single threaded application.

2 Comments

There's also ConcurrentHashMap.putIfAbsent() that doesn't have an equivalent in the old Hashtable. And when you only read from a ConcurrentHashMap, there are no locks, contrary to the Hashtable for which all operations are simply synchronized.
@FrankPavageau added your very useful comments in the Original answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.