Java ConcurrentHashMap | clear()

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()
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// clear() method
  
import java.util.concurrent.ConcurrentHashMap;
import java.util.*;
  
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);
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// clear() method
  
import java.util.concurrent.ConcurrentHashMap;
import java.util.*;
  
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);
    }
}

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.