HashMap Vs HashTable Vs SynchronizedMap Vs ConcurrentHashMap. There are number of questions can be raised in your mind, when we think about
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
HashMapin 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
Hashtablein 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
- similar to
- The only difference between
Hashtableand Synchronized Map is that later is not a legacy and you can wrap any Map to create it’s synchronized version by using
4. ConcurrentHashMap in Java
ConcurrentHashMapin 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.
ConcurrentHashMapin 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.
ConcurrentHashMapin Java doesn’t throw a
ConcurrentModificationExceptionif one thread tries to modify it while another is iterating over it.
ConcurrentHashMapdoes not allow NULL values, so key can not be null in
ConcurrentHashMapdoesn’t throw a
ConcurrentModificationExceptionif 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.
|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.|
|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
CopyOnWriteArrayListin 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 use
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.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
ConcurrentHashMapis 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
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.
ConcurrentHashMapuses 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.