Difference Between Hashmap and ConcurrentHashMapHashMap 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:
Let's discuss some examples to understand the behavior of HashMap and ConcurrentHashMap: Example of HashMap and Concurrent HashMapConsider 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 HashMapThe advantages of using ConcurrentHashMap are as follows:
Let's see some direct comparisons of HashMap and ConcurrentHashMap on the basis of different parameters:
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. Next TopicDifference between List and ArrayList |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India