当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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<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()



相关用法


注:本文由纯净天空筛选整理自RICHIK BHATTACHARJEE大神的英文原创作品 ConcurrentHashMap isEmpty() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。