當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。