Java AbstractMap isEmpty()方法及示例

Java AbstractMap isEmpty()方法及示例

AbstractMap类的AbstractMap.isEmpty()方法用于检查地图是否为空的。如果地图中没有键值对或映射,该方法返回True,否则返回False。

语法

AbstractMap.isEmpty()

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

返回值: 如果地图是空的或者不包含任何映射对,该方法返回布尔值true,否则返回布尔值false。

下面的程序说明了AbstractMap.isEmpty()方法的工作。

程序1: 将字符串值映射到整数键。

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

输出。

The Mappings are: {4=15, Geeks=20, You=30, Welcomes=25}
Is the map empty? false

程序2: 对于一个空的HashMap

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

输出。

The Mappings are: {}
Is the map empty? true

注意: 同样的操作可以通过不同数据类型的变化和组合对任何类型的Mapping进行操作。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程