• Tags         
  • 2018-08-07  22:19:22        
  • 79 °C    

    IdentityHashMap类的java.util.IdentityHashMap.isEmpty()方法用于检查map的空白。如果map中没有键值对或映射,则该方法返回True,否则返回False。

    句法:

    Identity_Hash_Map.isEmpty()

    参数:该方法不接受任何参数。

    返回值:如果映射为空或者不包含任何映射对,则该方法返回布尔值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

     
    转载请保留页面地址:https://www.breakyizhan.com/java/5333.html