WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java Collections · Intermediate · question 22 of 100

What is the difference between ConcurrentHashMap and Collections.synchronizedMap in Java Collections?

📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

ConcurrentHashMap and Collections.synchronizedMap are both implementations of the Map interface in the Java Collections Framework, and they both provide thread-safe access to the Map’s contents. However, there are some key differences between the two:

Locking mechanism: ConcurrentHashMap uses a fine-grained locking mechanism to achieve thread-safety, which allows multiple threads to access and modify the Map concurrently, while still ensuring that the Map remains consistent. Collections.synchronizedMap uses a coarse-grained locking mechanism, which locks the entire Map when any thread wants to access or modify it.

Performance: ConcurrentHashMap generally has better performance than Collections.synchronizedMap when multiple threads are accessing and modifying the Map concurrently, because its fine-grained locking mechanism allows for greater concurrency. Collections.synchronizedMap, on the other hand, can have performance issues when multiple threads are accessing the Map concurrently, because the entire Map is locked during each access or modification.

Null values: ConcurrentHashMap allows null keys and null values, while Collections.synchronizedMap allows one null key and any number of null values.

Here’s an example of how to use ConcurrentHashMap and Collections.synchronizedMap in Java:

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Example {
    public static void main(String[] args) {
        // create a ConcurrentHashMap of strings
        Map<String, String> myConcurrentHashMap = 
                new ConcurrentHashMap<>();
        myConcurrentHashMap.put("key1", "value1");
        myConcurrentHashMap.put("key2", "value2");
        myConcurrentHashMap.put(null, "value3");
        
        // create a synchronized HashMap of strings
        Map<String, String> mySynchronizedMap =
               Collections.synchronizedMap(new HashMap<>());
        mySynchronizedMap.put("key1", "value1");
        mySynchronizedMap.put("key2", "value2");
        
        // iterate over the elements of the ConcurrentHashMap
        for (Map.Entry<String, String> entry : 
                    myConcurrentHashMap.entrySet()) {
          System.out.println(entry.getKey() + ": " 
                + entry.getValue());
        }
        
        // iterate over the elements of the synchronized HashMap
        synchronized (mySynchronizedMap) {
          for (Map.Entry<String, String> entry :
                            mySynchronizedMap.entrySet()) {
            System.out.println(entry.getKey() + ": " 
                 + entry.getValue());
          }
        }
    }
}

In this example, we create a ConcurrentHashMap of strings called "myConcurrentHashMap" and add three key-value pairs to it, including one with a null key. We also create a synchronized HashMap of strings called "mySynchronizedMap" using the Collections.synchronizedMap() method and add two key-value pairs to it. We then iterate over the elements of both Maps using the entrySet() method and use the println() method to print out each key-value pair. Note that we use the synchronized keyword to lock the synchronized HashMap during iteration, because it is not thread-safe by default.

The output of this program would be:

null: value3
key1: value1
key2: value2
key1: value1
key2: value2

As you can see, ConcurrentHashMap allows null keys and values, while synchronized HashMap allows one null key and any number of null values.

In summary, ConcurrentHashMap and Collections.synchronizedMap are both implementations of the Map interface that provide thread-safe access to the Map’s contents, but they differ in their locking mechanism, performance, and handling of null values. Which one to use depends on the specific use case and requirements of your program.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Collections interview — then scores it.
📞 Practice Java Collections — free 15 min
📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

All 100 Java Collections questions · All topics