ConcurrentHashMap vs HashMap in Java

1. Introduction

In Java, HashMap and ConcurrentHashMap are two different types of collections that store items in key-value pairs. 

HashMap is a part of the Java Collections Framework and is not synchronized, which means it is not safe for use by multiple threads without external synchronization.

ConcurrentHashMap is a concurrent collection designed for thread-safe operations and is part of the Java Concurrency Utilities.

2. Key Points

1. HashMap allows one null key and multiple null values, whereas ConcurrentHashMap does not allow any null key or value.

2. HashMap is not synchronized and can cause issues if used in a multi-threaded environment without proper synchronization.

3. ConcurrentHashMap is thread-safe and does not lock the whole map when being read or written, which provides higher throughput in concurrent applications.

4. The iterators of HashMap are fail-fast, meaning they can throw a ConcurrentModificationException if the map is modified during iteration. On the other hand, the iterators of ConcurrentHashMap are weakly consistent and do not throw such exceptions.

3. Differences: ConcurrentHashMap vs HashMap in Java

HashMap ConcurrentHashMap
Not thread-safe. Thread-safe.
Allows one null key and multiple null values. Does not allow null keys or values.
Can be synchronized manually using Collections.synchronizedMap(). Natively thread-safe without needing additional synchronization wrappers.
Iterators are fail-fast. Iterators are weakly consistent and do not throw ConcurrentModificationException.

4. Example


// Importing the necessary classes
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MapComparison {
    public static void main(String[] args) {
        // Step 1: Create a HashMap
        Map<String, String> hashMap = new HashMap<>();
        // Step 2: Create a ConcurrentHashMap
        Map<String, String> concurrentHashMap = new ConcurrentHashMap<>();

        // Step 3: Put some key-value pairs into the HashMap
        hashMap.put("apple", "red");
        hashMap.put("banana", "yellow");

        // Step 4: Put some key-value pairs into the ConcurrentHashMap
        concurrentHashMap.put("apple", "green");
        concurrentHashMap.put("banana", "yellow");

        // Step 5: Print the contents of both maps
        System.out.println("HashMap: " + hashMap);
        System.out.println("ConcurrentHashMap: " + concurrentHashMap);
    }
}

Output:

HashMap: {apple=red, banana=yellow}
ConcurrentHashMap: {apple=green, banana=yellow}

Explanation:

1. The HashMap and ConcurrentHashMap classes are imported.

2. A HashMap is created to demonstrate its basic usage.

3. A ConcurrentHashMap is created, showcasing its thread-safe capability.

4. Key-value pairs are added to both the HashMap and ConcurrentHashMap.

5. The HashMap and ConcurrentHashMap are printed out, showing they both maintain a collection of key-value pairs.

6. It’s important to note that while both maps look similar when printed, the ConcurrentHashMap would provide thread safety during concurrent access, which is not shown in this single-threaded example.

Comments