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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

The Mappings are: {100=Geeks, 101=for, 102=Geeks}

Program 2:In this program the ConcurrentHashMap is non-empty.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

The ConcurrentHashMap is empty.

Reference : https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html#isEmpty()



My Personal Notes arrow_drop_up


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.