ConcurrentHashMap isEmpty() Method in Java
The isEmpty() method of java.util.concurrent.ConcurrentHashMap is an in-built function in Java which checks if this map contains any key-value mappings and returns a boolean value.
Syntax:
public boolean isEmpty()
Return Value: The function returns a boolean value. It returns true if the ConcurrentHashMap is empty and returns false otherwise.
Below programs illustrate the use of isEmpty() method :
Program 1: In this program the ConcurrentHashMap is non-empty.
// Java Program Demonstrate isEmpty() // method of ConcurrentHashMap */ import java.util.concurrent.*; class ConcurrentHashMapDemo { public static void main(String[] args) { ConcurrentHashMap<Integer, String> chm = new ConcurrentHashMap<Integer, String>(); chm.put( 100 , "Geeks" ); chm.put( 101 , "for" ); chm.put( 102 , "Geeks" ); // Checking for the emptiness of Map if (chm.isEmpty()) { System.out.println( "The ConcurrentHashMap" + " is empty." ); } else { // Displaying the ConcurrentHashMap System.out.println( "The Mappings are: " + chm); } } } |
The Mappings are: {100=Geeks, 101=for, 102=Geeks}
Program 2:In this program the ConcurrentHashMap is non-empty.
// Java Program Demonstrate isEmpty() // method of ConcurrentHashMap */ import java.util.concurrent.*; class GFG { public static void main(String[] args) { ConcurrentHashMap chm = new ConcurrentHashMap(); // Checking for the emptiness of Map if (chm.isEmpty()) { System.out.println( "The ConcurrentHashMap" + " is empty." ); } else { // Displaying the ConcurrentHashMap System.out.println( "The Mappings are: " + chm); } } } |
The ConcurrentHashMap is empty.
Reference : https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html#isEmpty()