5 things to know about ConcurrentHashMap in Java

1. What is a ConcurrentHashMap

 
ConcurrentHashMap class is a concrete implementation of the ConcurrentMap interface.
 
ConcurrentHashMap doesn’t implement thread safety by using Java’s synchronization/locking abilities and therefore provides better performance than a Hashtable
 
ConcurrentHashMap is similar to Hashtable as it has same functional specification as Hashtable, e.g. doesn’t allow duplicates or null key, elements aren’t returned in predictable order and both are thread safe.
 
But unlike Hashtable, the retrieval operation in ConcurrentHashMap does not create any locks and does not support locking the entire table to prevent all access.
 

ConcurrentHashMap allows us to optimize its performance by providing a concurrency level in constructor depending on how many different threads will be updating its contents.
 


ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel)

 

2. Difference between ConcurrentHashMap vs. HashMap

 
HashMap is unsynchronized. We can synchronize HashMap using Collections.synchronizedMap().But this locks up the entire hashmap and can cause serious performance issues when multiple threads are trying to access the hashmap.
 
ConcurrentHashMap performs better in a multi-threaded environment as it does not block itself to be access by a single thread.
 
Instead, it allows multiple threads to read its values and only few threads to modify its values.
 
Also, unlike HashMap, ConcurrentHashMap does not allow null to be used as a key or value.
 

3. Drawbacks of ConcurrentHashMap

 
ConcurrentHashMap doesn’t lock the entire collection while performing modification. So, it may not provide accurate information about its size using size() operation.
 
 

Creating a ConcurrentHashMap

 
We can create a ConcurrentHashMap class using following constructors:
 

How to create a ConcurrentHashMap ?

Creates a new, empty map with the default initial table size (16).
 
Here the code to create an empty ConcurrentHashMap with default size:
 


  ConcurrentMap<Integer,String> concurrentMap = new ConcurrentHashMap<Integer,String>();

 
ConcurrentHashMap also provides several other constructors that let us define an initialCapacity, loadFactor or use mapping from an existing map to build ConcurrentHashMap.
 
We can also use the constructor that allows us to mention the desired concurrency level.

ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel)

 

4. Using put() vs putIfAbsent() method in ConcurrentHashMap

 
If a key is not associated with a value, it can be associated with a value with following logic :
 

  if (!map.containsKey(key))
     return map.put(key, value);
   else
     return map.get(key);

 
But, when multiple threads access this, this won’t work as expected.
 
For this, ConcurrentHashMap provides a putIfAbsent() method that associates the key with a value if not already associated.
 
 

5. put() vs replace() method in ConcurrentHashMap

 
If we need to replace the value for an existing key, we can normally do so with following code :
 

   if (map.containsKey(key)) {
     return map.put(key, value);
   } 

 
However, in multithreaded environment, this can lead to race condition.
 
For this, ConcurrentHashMap provides a method replace() to replace the value for an existing key.
 
We can use it as follows :
 
  map.replace(key, value);

 
 

Summary of ConcurrentHashMap features

 
– ConcurrentHashMap is similar to Hashtable, but performs better in a multi-threaded environment as it does not block itself to be access by a single thread
 
– It does not allow duplicate keys.
 
– It does not allow null to be used as a key or value.
 
– Iterators of ConcurrentHashMap don’t throw a ConcurrentModificationException, so we don’t need to lock the collection while iterating it.

 
 

You may also like following articles on Java Collections:

 
 

© 2016 – 2019, https:. All rights reserved. On republishing this post, you must provide link to original post

8 comments

  1. […] ConcurrentHashMap in Java […]

  2. […] ConcurrentHashMap in Java […]

  3. […] ConcurrentHashMap in Java […]

  4. […] ConcurrentHashMap in Java […]

  5. […] ConcurrentHashMap in Java […]

Leave a Reply.. code can be added in <code> </code> tags

%d bloggers like this: