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


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


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

用法:

Identity_Hash_Map.isEmpty()

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


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

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

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

示例2:對於空的IdentityHashMap

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

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

Java.util.IdentityHashMap類的所有方法



相關用法


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