Java ConcurrentHashMap isEmpty()方法

来源:这里教程网 时间:2026-02-17 20:42:35 作者:

Java ConcurrentHashMap isEmpty()方法

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

语法

public boolean isEmpty()

返回值: 该函数返回一个布尔值。如果ConcurrentHashMap为空,则返回真,否则返回假。

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

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

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

程序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://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html#isEmpty()

相关推荐