HashMap Vs HashTable Vs SynchronizedMap Vs ConcurrentHashMap. There are number of questions can be raised in your mind, when we think about HashMap
vs Hashtable
java, SynchronizedMap
and ConcurrentHashMap
.
Before moving directly to difference between HashMap vs HashTable java, let us understand each of the important java classes as:
- HashMap in Java
- HashTable in Java
- SynchronizedMap in Java
- ConcurrentHashMap in Java
All of the above java collection framework classes, uses Map data structure to keep the data but one main difference is locking. Let us go one by one basic difference between HashMap vs HashTable java, SynchronizedMap ConcurrentHashMap Java.
1. HashMap in Java
HashMap
in Java 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 in Java
Hashtable
in Java 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 in Java
- SynchronizedMap in Java 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 usingCollections.synchronizedMap()
method.
4. ConcurrentHashMap in Java
ConcurrentHashMap
in Java 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
in java 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
in Java doesn’t throw aConcurrentModificationException
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 inConcurrentHashMap
ConcurrentHashMap
doesn’t throw aConcurrentModificationException
if one thread tries to modify it, while another is iterating over it.
5. Difference between Hashtable, HashMap and ConcurrentHashMap
Following are the basic differences between HashTable HashMap ConcurrentHashMap. All of these classes in java is very much important from development point of view and interview point of view too.
Hashtable | HashMap | ConcurrentHashMap | |
Thread-safe | Yes (synchronized keyword in all methods to provide thread safety) | No (only suitable for single threaded environment) | Yes (use java.concurrent.ReenterantLock to avoid concurrent access) |
Performance | slow, any operation blocks whole Map | so a new operation has to wait to finish of previous one., | Fast , as no locks are used, Faster than Hashtable , as locks are applied on different segments and any two operations across different segments are parallel. |
null | No | Yes | No |
Since | Yes | No, Introduced in JDK 1.2, | No \, added in JDK 1.5 with java.util.concurrent package |
Iterators | Fail Fast | Fail Fast | Fail safe |
6. Putting It Together
ConcurrentHashMap
andCopyOnWriteArrayList
in Java implementations provide much higher concurrency while preserving thread safety, with some minor compromises in their promises to callers. ConcurrentHashMap in Java and CopyOnWriteArrayList in Java are not necessarily useful everywhere you might useHashMap
orArrayList
, 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()
andCollections.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 preventConcurrentModificationException
. 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 externalsynchronization
. It is also very scalable because of stripped locking technique used in the internal implementation ofConcurrentHashMap
class. UnlikeHashtable
andSynchronized 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<k, v=””>[] 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)</k,>
Differences between some java classes
You can visit other differences between two java classes:
- Difference between HashMap and ConcurrentHashMap in java
- Difference Between HashMap and HashSet in Java
- HashMap Vs HashTable Vs SynchronizedMap Vs ConcurrentHashMap in Java
Suggestions are welcome always to improve this post.
Great article, most the info is informative… good
Please revise the table – ConcurrentHashMap is fail safe.
thanks your for you feedback krishana
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.