Java ConcurrentHashMap | clear() Last Updated : 20 Feb, 2023 Comments Improve Suggest changes Like Article Like 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); } } OutputMap 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); } } OutputMap before use of clear(): {Geeks3=120, Geek6=100, Geek5=110, Geek4=111, Geeks=100, Geek2=150} Map after use of clear(): {} Comment More infoAdvertise with us Next Article ConcurrentHashMap compute() method in Java with Examples B barykrg Follow Improve Article Tags : Misc Java Java - util package Java-ArrayList Practice Tags : JavaMisc Similar Reads ConcurrentHashMap in Java In Java, the ConcurrentHashMap is a thread-safe implementation of the Map interface. It allows multiple threads to read and write data simultaneously, without the need for locking the entire map. Unlike a regular HashMap, which is not thread-safe, ConcurrentHashMap ensures that the operations are th 15+ min read Java ConcurrentHashMap | clear() 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 3 min read ConcurrentHashMap compute() method in Java with Examples The compute(Key, BiFunction) method of ConcurrentHashMap class is used to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping is found). This method is used to atomically update a value for given key in ConcurrentHashMap.If the remapping funct 3 min read ConcurrentHashMap computeIfAbsent() method in Java with Examples The computeIfAbsent(Key, Function) method of ConcurrentHashMap class which attempts to compute its value using the given mapping function for specified key if key is not already associated with a value (or is mapped to null) and enter that computed value in map else null. If mapping function of this 3 min read ConcurrentHashMap contains() method in Java with Examples The contains() method of Java.util.ConcurrentHashMap is used to check whether if some key maps into the specified value in this table. It is a legacy method of performing a particular task. The operation is similar to containsValue() Method of ConcurrentHashMap. Syntax: ConcurrentHashMap.contains(Ob 2 min read ConcurrentHashMap containsKey() Method in Java The containsKey() method in Java's ConcurrentHashMap class is used to determine whether the map contains a given key. It has the following signature: boolean containsKey(Object key) where: key is the key to be searched for in the map. The containsKey() method works in a concurrent environment, which 3 min read ConcurrentHashMap containsValue() Method in Java The containsValue() method in Java's ConcurrentHashMap class is used to determine whether the map contains a given value. It has the following signature: boolean containsValue(Object value) where: value is the value to be searched for in the map.The containsValue() method works in a concurrent envir 3 min read ConcurrentHashMap elements() method in Java with Examples The elements() method of ConcurrentHashMap class in Java is used to get the enumeration of the values present in the table. Syntax: Enumeration enu = ConcurrentHashMap.elements() Parameters: The method does not take any parameters. Return value: The method returns an enumeration of the values of the 2 min read ConcurrentHashMap entrySet() method in Java with Examples The entrySet() method of ConcurrentHashMap in Java is used to create a set from the same elements contained in the concurrent hash map. It basically returns a set view of the concurrent hash map or we can create a new set and store the map elements into them. Syntax: ConcurrentHashMap.entrySet() Par 2 min read ConcurrentHashMap get() Method in Java The get() method in Java's ConcurrentHashMap class is used to retrieve the value associated with a given key. It has the following signature: V get(Object key) where: key is the key whose associated value is to be retrieved.The get() method works in a concurrent environment, which means that it can 3 min read Like