Open In App
Related Articles

Java ConcurrentHashMap | clear()

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The clear() method in Java’s ConcurrentHashMap class is used to remove all the key-value pairs from the map. It has the following signature:

The clear() method works in a concurrent environment, which means that it can be called from multiple threads without causing any data race or synchronization issues. When the method is called, it acquires the lock on all the segments of the map and removes all the key-value pairs.

After the clear() method is called, the map becomes empty, i.e., its size() method returns 0.

Here is an example of using the clear() method:

Java

import java.util.concurrent.ConcurrentHashMap;
 
public class ConcurrentHashMapExample {
    public static void main(String[] args) {
        ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
        map.put("Alice", 25);
        map.put("Bob", 30);
        map.put("Charlie", 35);
 
        System.out.println(map); // {Charlie=35, Bob=30, Alice=25}
 
        map.clear();
        System.out.println(map); // {}
    }
}

                    

Output
{Bob=30, Alice=25, Charlie=35}
{}

In this example, we first create a ConcurrentHashMap and add three key-value pairs to it using the put() method. We then print out the contents of the map.

Next, we call the clear() method to remove all the key-value pairs from the map. We then print out the contents of the map again to verify that it is empty.

Prerequisite: ConcurrentHashmap Java clear() method The java.util.concurrentHashMap.clear() method is used to clear the mapping. It is used to remove the mapping from ConcurrentHashMap. 

Syntax:

public void clear()

Java

// Java program to demonstrate
// clear() method
 
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
 
public class ConcurrentHashMapExample {
 
    public static void main(String[] args)
    {
 
        // Creating a ConcurrentHashMap
        Map<String, String> my_cmmap
            = new ConcurrentHashMap<String, String>();
 
        // Inserting mappings in ConcurrentHashMap
        // with the help of put() method
        my_cmmap.put("1", "1");
        my_cmmap.put("2", "1");
        my_cmmap.put("3", "1");
        my_cmmap.put("4", "1");
        my_cmmap.put("5", "1");
        my_cmmap.put("6", "1");
 
        // Print the ConcurrentHashMap
        System.out.println("Map before use of clear(): \n"
                           + my_cmmap);
 
        // Now clear the map using clear()
        my_cmmap.clear();
 
        // Print the clea Map
        System.out.println("Map after use of clear(): "
                           + my_cmmap);
    }
}

                    

Output
Map before use of clear(): 
{1=1, 2=1, 3=1, 4=1, 5=1, 6=1}
Map after use of clear(): {}

Example 2: 

Java

// Java program to demonstrate
// clear() method
 
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
 
public class ConcurrentHashMapExample {
 
    public static void main(String[] args)
    {
        // Creating a ConcurrentHashMap
        Map<String, String> my_map
            = new ConcurrentHashMap<String, String>();
 
        // Inserting mappings in ConcurrentHashMap
        // with the help of put() method
        my_map.put("Geeks", "100");
        my_map.put("Geek2", "150");
        my_map.put("Geeks3", "120");
        my_map.put("Geek4", "111");
        my_map.put("Geek5", "110");
        my_map.put("Geek6", "100");
 
        // Print the ConcurrentHashMap
        System.out.println("Map before use of clear(): \n"
                           + my_map);
 
        // Now clear the map using clear()
        my_map.clear();
 
        // Print the cleared Map
        System.out.println("Map after use of clear(): "
                           + my_map);
    }
}

                    

Output
Map before use of clear(): 
{Geeks3=120, Geek6=100, Geek5=110, Geek4=111, Geeks=100, Geek2=150}
Map after use of clear(): {}


Last Updated : 20 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads