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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。