Difference Between Hashmap and ConcurrentHashMap
HashMap is a powerful data structure in Java used to store the key-pair values. It maps a value by its associated key. It allows us to store the null values and null keys. It is a non-synchronized class of Java collection. Whereas, ConcurrentHashMap is introduced as an alternative to the HashMap. The ConcurrentHashMap is a synchronized collection class.
The HashMap is non-thread-safe and can not be used in a Concurrent multi-threaded environment. Comparatively, ConcurrentHashMap is a thread-safe and specially designed for use in multi-threaded and Concurrent environment.
In this section, we will see the difference between HashMap and ConcurrentHashMap based on several parameters such as thread-safety, synchronization, performance, uses, etc.
Below are some key differences between HashMap and ConcurrentHashMap:
- As discussed above, the HashMap is a non-synchronized and non-Thread safe, while the ConcurrentHashMap is a synchronized and Thread-safe collection class. Though the ConcurrentHashMap can not match the synchronization level of Hashtable, it performs well for most of the practical cases.
- The HashMap can be synchronized using the Collection.syncronizedMap; It returns a collection that is almost equal to Hashtable.
- The synchronized HashMap is less scalable than the ConcurrentHashMap.
- In the multi-threaded environment, The ConcurrentHashMap has improved performance than Synchronized HashMap.
- In the single-threaded environment, The HashMap is slightly better than ConcurrentHashMap.
- In HashMap, if one thread is iterating the object and the other thread wants to modify the objects, we will get a ConcurrentModificationException runtime exception. But, in ConcurrentHashMap, one thread can perform modification while the other thread is running.
- The HashMap is introduced in Java 2 (JDK 1.2) & ConcurrentHashMap is introduced in Java 5 (JDK 1.5).
Let’s discuss some examples to understand the behavior of HashMap and ConcurrentHashMap:
Example of HashMap and Concurrent HashMap
Consider the below example to understand the behavior of HashMap:
Example1:
Output:
{null=Sofia, 100=Stark, 101=Michale, 102=Ani}
Now understand the same example using ConcuurentHashMap:
Using ConcurrentHashMap:
Output:
Exception in thread "main" java.lang.NullPointerException at java.util.concurrent.ConcurrentHashMap.putVal(ConcurrentHashMap.java:1011) at java.util.concurrent.ConcurrentHashMap.put(ConcurrentHashMap.java:1006) at ConcurrentHashMapExample.main(ConcurrentHashMapExample.java:9)
As we can see from the above examples, the null values are allowed for key-pair in HashMap, but, in ConcurrentHashMap, we can not use null values for key-value combination; it will throw a NullPointerException.
Example2:
In HashMap, if one thread is iterating the object and the other thread will try to iterate the object, it will throw a runtime exception. But, in ConcurrentHashMap, it is possible to iterate the objects simultaneously by two or more threads.
Consider the below example:
Using HashMap:
// Java program to add objects simultaneously by two threads using HashMap
Output:
100=X 101=Y Exception in thread "main" java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextNode(HashMap.java:1445) at java.util.HashMap$EntryIterator.next(HashMap.java:1479) at java.util.HashMap$EntryIterator.next(HashMap.java:1477) at HashMapExample2.main(HashMapExample2.java:27)
From the above example, when the child thread tries to add objects simultaneously with other threads, it throws a runtime exception. Let’s execute the same code using ConcurrentHashMap:
Using ConcurrentHashMap:
Output:
100=X 101=Y 102=Z 103=D {100=X, 101=Y, 102=Z, 103=D}
From the above example, in ConcurrentHashMap, we can easily add objects simultaneously using two different threads. But, it is not possible in HashMap.
Advantages of ConcurrentHashMap over HashMap
The advantages of using ConcurrentHashMap are as follows:
- It provides very high concurrency in a multi-threaded environment.
- The read operation can be very fast when the write operation is done with a lock.
- It provides No object-level Locking.
- It uses a multitude of locks.
- It allows other threads to iterate the objects when one thread is iterating.
- It is thread-safe, especially in the case of multi-threading.
Let’s see some direct comparisons of HashMap and ConcurrentHashMap on the basis of different parameters:
Parameters | HashMap | ConcurrentHashMap |
---|---|---|
Synchronization | Non-synchronized | synchronized |
Thread-safety | Not thread-safe | Thread-safe |
Iterator | It is fail-fast and throws an exception during iteration | It is fail-safe and performs iteration by multiple threads |
Null Values | It allows for storing null keys and values. | It does not allow to store null key/values. |
Performance | faster | Slower than Hashmap |
Summary:
We have discussed all the major differences between HashMap and ConcurrentHashMap. Adding a final note to this discussion, we would like to say that the ConcuurentHashMap is beneficial in a multi-threaded environment and performs better than HashMap. It provides thread-safety, scalability, and synchronization. For the single-threaded environment, the HashMap performs slightly better than ConcurrentHashMap.