• Java
    • JAXB Tutorial
      • What is JAXB
      • JAXB Marshalling Example
      • JAXB UnMarshalling Example
  • Spring Tutorial
    • Spring Core Tutorial
    • Spring MVC Tutorial
      • Quick Start
        • Flow Diagram
        • Hello World Example
        • Form Handling Example
      • Handler Mapping
        • BeanNameUrlHandlerMapping
        • ControllerClassNameHandlerMapping
        • SimpleUrlHandlerMapping
      • Validation & Exception Handling
        • Validation+Annotations
        • Validation+ResourceBundle
        • @ExceptionHandler
        • @ControllerAdvice
        • Custom Exception Handling
      • Form Tag Library
        • Textbox Example
        • TextArea Example
        • Password Example
        • Dropdown Box Example
        • Checkboxes Example
        • Radiobuttons Example
        • HiddenValue Example
      • Misc
        • Change Config file name
    • Spring Boot Tutorial
  • Hibernate Tutorial
  • REST Tutorial
    • JAX-RS REST @PathParam Example
    • JAX-RS REST @QueryParam Example
    • JAX-RS REST @DefaultValue Example
    • JAX-RS REST @Context Example
    • JAX-RS REST @MatrixParam Example
    • JAX-RS REST @FormParam Example
    • JAX-RS REST @Produces Example
    • JAX-RS REST @Consumes Example
    • JAX-RS REST @Produces both XML and JSON Example
    • JAX-RS REST @Consumes both XML and JSON Example
  • Miscellaneous
    • JSON Parser
      • Read a JSON file
      • Write JSON object to File
      • Read / Write JSON using GSON
      • Java Object to JSON using JAXB
    • CSV Parser
      • Read / Write CSV file
      • Read/Parse/Write CSV File – OpenCSV
      • Export data into a CSV File
      • CsvToBean and BeanToCsv – OpenCSV

JavaInterviewPoint

Java Development Tutorials

Java ConcurrentHashMap | ConcurrentHashMap vs HashMap

October 29, 2019 by javainterviewpoint Leave a Comment

ConcurrentHashMap is a class introduced in Java 1.5 Version which implements the ConcurrentMap interface. The ConcurrentHashMap is simply a concurrent version of the HashMap, the functionality is also similar to that of a HashMap except for the internal concurrency. In this article, we will get to know what is Java ConcurrentHashMap and it’s usage, along with it we will also have a look at the difference between HashMap and ConcurrentHashMap and difference between ConcurrentHashMap, SynchronizedMap, and HashTable.

Why we need ConcurrentHashMap?

Whenever we say Map, we already know two popular implementations HashMap and HashTable? Then Why we need ConcurrentHashMap?

Along with this, there might rise a lot of questions like?

If HashMap is not considered as ThreadSafe, then we can simply make it Synchronized using the Collections.synchronizedMap() method. Even if this approach didn’t work, then we have HashTable which is by default ThreadSafe. Then, What is the additional feature that a ConcurrentHashMap provides?

The problem with SynchronizedMap and HashTable is that it locks the entire object, so only one thread will be able to access the Map object even for the read operation, whereas ConcurrentHashMap uses a different type of locking mechanism which allows multiple threads to read and write concurrently without compromising the Thread Safety.

Java ConcurrentHashMap

Java ConcurrentHashMap

How ConcurrentHashMap works internally?

We all know that ConcurrentHashMap functions exactly like a HashMap, but defers on the locking mechanism.

In order to understand it better, let’s recall the internal implementation of HashMap. The HashMap stores the values in buckets and there are 16 buckets by default.

ConcurrentHashMap calls each bucket as Segment and provides a separate lock for each Segment and hence the default concurrency level is also 16. In the ConcurrentHashMap code itself, we could see there are two constants defined.

static final int DEFAULT_INITIAL_CAPACITY = 16;
static final int DEFAULT_CONCURRENCY_LEVEL = 16;

So whenever a thread needs to do some update operation on the Map, it need not acquire the lock on the entire object all it needs to do is just get the lock for the particular segment alone. Since there 16 segment locks available, at any point in time 16 threads can concurrently perform the update operation.

Furthermore, the thread doesn’t need any sort lock in order to perform read operation on the ConcurrentHashMap, so in simple words, we can say any number of threads can perform read operation and 16 threads can perform update operation simultaneously at a given time.

Constructors on ConcurrentHashMap

  1.  ConcurrentHashMap() – This constructor creates an empty ConcurrentHashMap with initial capacity (16), load factor (0.75) and concurrency level (16).
  2. new ConcurrentHashMap(int initialCapacity) – This constructor creates an empty ConcurrentHashMap with the specified initial capacity, and load factor (0.75) and concurrency level (16).
  3. ConcurrentHashMap(int initialCapacity, float loadFactor) – This constructor creates an empty ConcurrentHashMap with the specified initial capacity, specified load factor and default concurrency level (16).
  4. ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) – This constructor creates an empty ConcurrentHashMap with the specified initial capacity, load factor, and concurrency level.
  5. ConcurrentHashMap(Map<? extends K,? extends V> m) – This constructor creates a ConcurrentHashMap from the existing map which is passed to it.

Java ConcurrentHashMap Example

Let’s take a look into a simple Java ConcurrentHashMap example, we will also see some of the new methods such as putIfAbsent(), remove(), replace() which are added to the ConcurrentMap interface.

package com.javainterviewpoint.concurrenthashmap;

import java.util.concurrent.ConcurrentHashMap;

public class ConcurrentHashMapExample
{
	public static void main(String[] args)
	{
		ConcurrentHashMap<Integer, String> chm = new ConcurrentHashMap<Integer, String>();
		
		chm.put(101, "Jim");
		chm.put(102, "Tim");
		chm.putIfAbsent(103, "Tom");
		chm.putIfAbsent(104, "Jerry");
		chm.putIfAbsent(105, "Nick");
		
		/** Newly added in ConcurrentMap interface, 
		Wont be added because there is any entry already exist for 102 **/
		chm.putIfAbsent(102, "Timmy"); 
		
		
		/** Newly added in ConcurrentMap interface, 
		 removes the entry only when both key and value matches
		Nothing will happen, though key matches value doesn't match **/
		chm.remove(105, "Jip"); 
		System.out.println(chm);
		
		// Removes 104 entity
		chm.remove(104,"Jerry");
		System.out.println(chm);
		
		// Replaces Nick with the value JIP
		chm.replace(105, "Nick", "JIP");
		
		System.out.println(chm);
	}
}

null not allowed in ConcurrentHashMap

Even though one null key and multiple null values are allowed in HashMap, ConcurrentHashMap doesn’t allow either null key or null value.

package com.javainterviewpoint.concurrenthashmap;

import java.util.concurrent.ConcurrentHashMap;

public class ConcurrentHashMapExample
{
	public static void main(String[] args)
	{
		ConcurrentHashMap<Integer, String> chm = new ConcurrentHashMap<Integer, String>();
		
		chm.put(101, "Jim");
		chm.put(102, "Tim");
		chm.putIfAbsent(103, "Tom");
		chm.putIfAbsent(104, "Jerry");
		chm.putIfAbsent(105, "Nick");
		chm.put(null, "James");
		
		System.out.println(chm);
	}
}

Since in ConcurrentHashMap multiple threads will do modifications to the Map there might be a possibility that key k might be deleted in between the containsKey(k) and get(k) calls.

If we try to add a null key, we will get NullPointerException.

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 com.javainterviewpoint.concurrenthashmap.ConcurrentHashMapExample.main(ConcurrentHashMapExample.java:16)

No ConcurrentModificationException / Fail-Safe iterator

The iterator of ConcurrentHashMap is fail-safe, which means that the iterator will not throw ConcurrentModificationException when the underlying collection is modified during iteration.

When we try to add a new entity to the HashMap while iterating we will be getting ConcurrentModificationException

package com.javainterviewpoint.concurrenthashmap;

import java.util.HashMap;
import java.util.Iterator;

public class HashMapExample
{
	public static void main(String[] args)
	{
		HashMap<Integer, String> hm = new HashMap<Integer, String>();
		
		hm.put(1, "One");
		hm.put(2, "Two");
		hm.putIfAbsent(3, "Three");
		hm.putIfAbsent(4, "Four");
		hm.putIfAbsent(5, "Five");

		Iterator it = hm.keySet().iterator();
		
		while(it.hasNext())
		{
			Integer key = (Integer) it.next();
			System.out.println("Key: "+key+" Value: "+hm.get(key));
			if(key == 3)
			{
				hm.put(6,"Six");
			}
		}
		System.out.println(hm);
	}
}

Output:

Key: 1 Value: One
Key: 2 Value: Two
Key: 3 Value: Three
Exception in thread "main" java.util.ConcurrentModificationException
	at java.util.HashMap$HashIterator.nextNode(HashMap.java:1445)
	at java.util.HashMap$KeyIterator.next(HashMap.java:1469)
	at com.javainterviewpoint.concurrenthashmap.HashMapExample.main(HashMapExample.java:22)

Whereas in the case of ConcurrentHashMap we will not ConcurrentModificationException, Let’s change the above code to ConcurrentHashMap

package com.javainterviewpoint.concurrenthashmap;

import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

public class ConcurrentHashMapExample
{
	public static void main(String[] args)
	{
		ConcurrentHashMap<Integer, String> chm = new ConcurrentHashMap<Integer, String>();
		
		chm.put(1, "One");
		chm.put(2, "Two");
		chm.putIfAbsent(3, "Three");
		chm.putIfAbsent(4, "Four");
		chm.putIfAbsent(5, "Five");

		Iterator it = chm.keySet().iterator();
		
		while(it.hasNext())
		{
			Integer key = (Integer) it.next();
			System.out.println("Key: "+key+" Value: "+chm.get(key));
			if(key == 3)
			{
				chm.put(6,"Six");
			}
		}
		System.out.println(chm);
	}
}

Output:

Key: 1 Value: One
Key: 2 Value: Two
Key: 3 Value: Three
Key: 4 Value: Four
Key: 5 Value: Five
Key: 6 Value: Six
{1=One, 2=Tow, 3=Three, 4=Four, 5=Five, 6=Six}

Difference between HashMap and ConcurrentHashMap

Let’s understand the difference between HashMap and ConcurrentHashMap

HashMap vs ConcurrentHashMap

HashMap ConcurrentHashMap
HashMap is not Synchronized ConcurrentHashMap is Synchronized
HashMap is not Thread Safe ConcurrentHashMap is Thread Safe
In HashMap 1 null key and multiple null values are allowed ConcurrentHashMap does not allow either null key or a null value if we try to add we will get NullPointerException
During iteration, when the underlying HashMap is modified, we will get ConcurrentModificationException During iteration, we can make modifications to underlying ConcurrentHashMap, we will not get ConcurrentModificationException
The Iterator of HashMap is Fail-Fast The Iterator of ConcurrentHashMap is Fail-Safe
Performance of HashMap is comparatively higher than ConcurrentHashMap as HashMap is not Thread Safe Performance of ConcurrentHashMap is comparatively lower than HashMap, as ConcurrentHashMap is Thread Safe
Introduced in 1.2 Version of Java Introduced in 1.5 Version of Java

Difference between – ConcurrentHashMap vs SynchronizedMap vs HashTable

ConcurrentHashMap SynchronizedMap [Collections.synchronizedMap()] HashTable
We will get thread safety without locking the entire Map object, just Segment / Bucket level lock is enough We will get thread safety by locking the complete Map Object We will get thread safety by locking the complete Map Object
At a time multiple threads are allowed to perform any operation on the Map object At a time only one thread is allowed to perform any operation on the Map object At a time only one thread is allowed to perform any operation on the Map object
Read operation can be performed without a lock and Write operation can be performed with bucket/segment level lock Both Read and Write operation requires the lock on the complete Map object Both Read and Write operation requires the lock on the complete Map object
During iteration, we are allowed to make a modification to the underlying ConcurrentHashMap and we will not get ConcurrentModificationException During iteration, we are allowed to make a modification to the underlying SynchronizedMap and we will get ConcurrentModificationException During iteration, we are allowed to make a modification to the underlying HashTable and we will get ConcurrentModificationException
Performance is comparatively high when compared with SynchronizedMap and HashTable because of the bucket level locking mechanism Performance is comparatively low when compared with ConcurrentHashMap because of whole Map object lock Performance is comparatively low when compared with ConcurrentHashMap because of whole Map object lock
The Iterator of ConcurrentHashMap is Fail-Safe, that is during iteration when the underlying collection is modified we will not get ConcurrentModificationException The Iterator of SynchronizedMap is Fail-Fast, that is during iteration when the underlying collection is modified we will get ConcurrentModificationException The Iterator of HashTable is Fail-Fast, that is during iteration when the underlying collection is modified we will get ConcurrentModificationException
For both key and value null is not allowed 1 null key and multiple null values are allowed For both key and value null is not allowed
Introduced in 1.5 Version of Java Introduced in 1.2 Version of Java Introduced in 1.0 Version of Java

Happy Learning!!

Other interesting articles which you may like …

  • Difference between new operator vs newInstance() method in Java
  • Java Static Import
  • Java – How System.out.println() really work?
  • Java Ternary operator
  • Java newInstance() method
  • Java Constructor.newInstance() method Example
  • Parameterized Constructor in Java
  • Sort Objects in a ArrayList using Java Comparator
  • Sort Objects in a ArrayList using Java Comparable Interface
  • Difference between equals() and ==
  • Difference Between Interface and Abstract Class in Java
  • Difference between fail-fast and fail-safe Iterator
  • Difference between Enumeration and Iterator ?
  • Difference between HashMap and Hashtable | HashMap Vs Hashtable
  • JVM Architecture – Understanding JVM Internals
  • Object and Object Class in Java
  • Difference between JDK, JRE and JVM
  • Components of Java Development Kit (JDK)
  • How to open .class file in Java
  • How to Set Classpath for Java in Windows
  • ClassNotFoundException Vs NoClassDefFoundError
  • How HashMap works in Java
  • How to make a class Immutable in Java
  • Polymorphism in Java – Method Overloading and Overriding
  • Types of polymorphism in Java
  • Types of Inheritance in Java
  • Java does not supports Multiple Inheritance Diamond Problem?

Filed Under: Core Java, Java, Java Interview Tagged With: ConcurrentHashMap, ConcurrentHashMap vs HashMap, ConcurrentHashMap vs HashTable, Java ConcurrentHashMap

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Java Basics

  • JVM Architecture
  • Object in Java
  • Class in Java
  • How to Set Classpath for Java in Windows
  • Components of JDK
  • Decompiling a class file
  • Use of Class.forName in java
  • Use Class.forName in SQL JDBC

Oops Concepts

  • Inheritance in Java
  • Types of Inheritance in Java
  • Single Inheritance in Java
  • Multiple Inheritance in Java
  • Multilevel Inheritance in Java
  • Hierarchical Inheritance in Java
  • Hybrid Inheritance in Java
  • Polymorphism in Java – Method Overloading and Overriding
  • Types of Polymorphism in java
  • Method Overriding in Java
  • Can we Overload static methods in Java
  • Can we Override static methods in Java
  • Java Constructor Overloading
  • Java Method Overloading Example
  • Encapsulation in Java with Example
  • Constructor in Java
  • Constructor in an Interface?
  • Parameterized Constructor in Java
  • Constructor Chaining with example
  • What is the use of a Private Constructors in Java
  • Interface in Java
  • What is Marker Interface
  • Abstract Class in Java

Java Keywords

  • Java this keyword
  • Java super keyword
  • Final Keyword in Java
  • static Keyword in Java
  • Static Import
  • Transient Keyword

Miscellaneous

  • newInstance() method
  • How does Hashmap works internally in Java
  • Java Ternary operator
  • How System.out.println() really work?
  • Autoboxing and Unboxing Examples
  • Serialization and Deserialization in Java with Example
  • Generate SerialVersionUID in Java
  • How to make a class Immutable in Java
  • Differences betwen HashMap and Hashtable
  • Difference between Enumeration and Iterator ?
  • Difference between fail-fast and fail-safe Iterator
  • Difference Between Interface and Abstract Class in Java
  • Difference between equals() and ==
  • Sort Objects in a ArrayList using Java Comparable Interface
  • Sort Objects in a ArrayList using Java Comparator

Follow

  • Coding Utils

Useful Links

  • Spring 4.1.x Documentation
  • Spring 3.2.x Documentation
  • Spring 2.5.x Documentation
  • Java 6 API
  • Java 7 API
  • Java 8 API
  • Java EE 5 Tutorial
  • Java EE 6 Tutorial
  • Java EE 7 Tutorial
  • Maven Repository
  • Hibernate ORM

About JavaInterviewPoint

javainterviewpoint.com is a tech blog dedicated to all Java/J2EE developers and Web Developers. We publish useful tutorials on Java, J2EE and all latest frameworks.

All examples and tutorials posted here are very well tested in our development environment.

Connect with us on Facebook | Privacy Policy | Sitemap

Copyright ©2023 · Java Interview Point - All Rights Are Reserved ·