Java中的 NavigableMap isEmpty() 方法
NavigableMap 接口的 isEmpty() 方法用于检查地图是否为空。如果映射中不存在键值对或映射,则该方法返回 True,否则返回 False。
句法:
NavigableMap.isEmpty()
参数:该方法不带任何参数。
返回值:如果映射为空或不包含任何映射对,则该方法返回布尔值 true,否则返回布尔值 false。
下面的程序说明了 isEmpty() 方法的工作:
程序 1:将字符串值映射到整数键。
// Java code to illustrate the isEmpty() method
import java.util.*;
public class NavigableMap_Demo {
public static void main(String[] args)
{
// Creating an empty NavigableMap
NavigableMap nav_map = new TreeMap();
// Mapping int values to string keys
nav_map.put("Geeks", 10);
nav_map.put("4", 15);
nav_map.put("Geeks", 20);
nav_map.put("Welcomes", 25);
nav_map.put("You", 30);
// Displaying the NavigableMap
System.out.println("The Mappings are: " + nav_map);
// Checking for the emptiness of Map
System.out.println("Is the map empty? " + nav_map.isEmpty());
}
}
输出:
The Mappings are: {4=15, Geeks=20, Welcomes=25, You=30}
Is the map empty? false
程序 2:对于一个空的 IdentityHashMap
// Java code to illustrate the isEmpty() method
import java.util.*;
public class NavigableMap_Demo {
public static void main(String[] args)
{
// Creating an empty NavigableMap
NavigableMap nav_map = new TreeMap();
// Displaying the NavigableMap
System.out.println("The Mappings are: " + nav_map);
// Checking for the emptiness of Map
System.out.println("Is the map empty? " + nav_map.isEmpty());
}
}
输出:
The Mappings are: {}
Is the map empty? true
注意:可以对任何类型的映射执行相同的操作,这些映射具有不同数据类型的变化和组合。
Java.util.IdentityHashMap 类的所有方法