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


Java WeakHashMap isEmpty()用法及代碼示例


HashMap類的java.util.WeakHashMap.isEmpty()方法用於檢查Map是否為空。如果映射中不存在鍵值對或映射,則該方法返回True,否則返回False。

用法:

Weak_Hash_Map.isEmpty()

參數:該方法不帶任何參數。


返回值:如果映射為空或不包含任何映射對,則此方法返回boolean true,否則返回boolean false。

以下示例程序旨在說明java.util.WeakHashMap.isEmpty()方法的用法:
示例1:將字符串值映射到整數鍵。

// Java code to illustrate the isEmpty() method 
import java.util.*; 
  
public class Weak_Hash_Map_Demo { 
    public static void main(String[] args) 
    { 
  
        // Creating an empty WeakHashMap 
        Map<String, Integer> weak_hash = new 
                     WeakHashMap<String, Integer>(); 
  
        // Mapping int values to string keys 
        weak_hash.put("Geeks", 10); 
        weak_hash.put("4", 15); 
        weak_hash.put("Geeks", 20); 
        weak_hash.put("Welcomes", 25); 
        weak_hash.put("You", 30); 
  
        // Displaying the WeakHashMap 
        System.out.println("The Mappings are: "+ 
                                      weak_hash); 
  
        // Checking for the emptiness of Map 
        System.out.println("Is the map empty? "+ 
                           weak_hash.isEmpty()); 
    } 
}
輸出:
The Mappings are: {Welcomes=25, 4=15, You=30, Geeks=20}
Is the map empty? false

示例2:對於空的WeakHashMap

// Java code to illustrate the isEmpty() method 
import java.util.*; 
  
public class Weak_Hash_Map_Demo { 
    public static void main(String[] args) 
    { 
  
        // Creating an empty WeakHashMap 
        Map<String, Integer> weak_hash = new 
                  WeakHashMap<String, Integer>(); 
  
        // Displaying the WeakHashMap 
        System.out.println("The Mappings are: "+ 
                                      weak_hash); 
  
        // Checking for the emptiness of Map 
        System.out.println("Is the map empty? "+ 
                            weak_hash.isEmpty()); 
    } 
}
輸出:
The Mappings are: {}
Is the map empty? true

注意:可以對具有不同數據類型的變化和組合的任何類型的映射執行相同的操作。

Java.util.WeakHashmap類的所有方法



相關用法


注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 WeakHashMap isEmpty() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。