Skip to content
Related Articles

Related Articles

ConcurrentHashMap isEmpty() Method in Java
  • Last Updated : 14 Sep, 2018
GeeksforGeeks - Summer Carnival Banner


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);
        }
    }
}
Output:
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);
        }
    }
}
Output:
The ConcurrentHashMap is empty.

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :