IdentityHashMap的isEmpty()方法在Java中的应用
java.util.IdentityHashMap类的IdentityHashMap.isEmpty()方法用于检查映射是否为空。该方法返回True如果地图中未包含键-值对映射,则返回False。
语法:
Identity_Hash_Map.isEmpty()
参数: 该方法不需要任何参数。
返回值: 如果地图为空或不包含任何映射对,则该方法返回布尔值true;否则返回布尔值false。
以下程序说明了java.util.IdentityHashMap.isEmpty()方法的工作原理:
程序1: 将字符串值映射到整数键。
// Java代码以演示isEmpty()方法
import java.util.*;
public class Identity_Hash_Map_Demo {
public static void main(String[] args)
{
// 创建一个空的IdentityHashMap
Map<String, Integer> identity_hash = new
IdentityHashMap<String, Integer>();
// 将整数值映射到字符串键
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);
// 显示IdentityHashMap
System.out.println("The Mappings are: "+
identity_hash);
// 检查地图是否为空
System.out.println("Is the map empty? "+
identity_hash.isEmpty());
}
}
映射是:{Geeks = 20,欢迎= 25,You = 30,4 = 15}
地图是否为空?假
程序2: 对于一个空的IdentityHashMap
// Java代码以演示isEmpty()方法
import java.util.*;
public class Identity_Hash_Map_Demo {
public static void main(String[] args)
{
// 创建一个空的IdentityHashMap
Map<String, Integer> identity_hash = new
IdentityHashMap<String, Integer>();
// 显示IdentityHashMap
System.out.println("The Mappings are: "+
identity_hash);
// 检查地图是否为空
System.out.println("Is the map empty? "+
identity_hash.isEmpty());
}
}
映射是:{}
地图是否为空?真
注意: 可以使用不同数据类型的变化和组合执行任何类型映射的相同操作。
Java.util.IdentityHashMap类的所有方法
极客教程