📜  Java中的 ConcurrentHashMap isEmpty() 方法

📅  最后修改于: 2022-05-13 01:55:06.066000             🧑  作者: Mango

Java中的 ConcurrentHashMap isEmpty() 方法


Java .util.concurrent.ConcurrentHashMap 的isEmpty()方法是Java中的一个内置函数,用于检查映射是否包含任何键值映射并返回一个布尔值。

句法:

public boolean isEmpty()

返回值:函数返回一个布尔值。如果 ConcurrentHashMap 为空,则返回 true,否则返回 false。

下面的程序说明了isEmpty()方法的使用:

程序 1:在这个程序中,ConcurrentHashMap 是非空的。

// Java Program Demonstrate isEmpty()
// method of ConcurrentHashMap */
  
import java.util.concurrent.*;
  
class ConcurrentHashMapDemo {
    public static void main(String[] args)
    {
  
        ConcurrentHashMap
            chm = new ConcurrentHashMap();
  
        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}

程序 2:在这个程序中,ConcurrentHashMap 是非空的。

// 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.

参考: https: Java/util/concurrent/ConcurrentHashMap.html#isEmpty()