检查 Java 中 IdentityHashMap 是否为空?
使用 isEmpty() 方法检查 Map 是否为空。
首先让我们创建一个 IdentityHashMap 并向其中添加一些元素
Map<String, Integer> m= new IdentityHashMap<String, Integer>();
m.put("1", 100);
m.put("2", 200);
m.put("3", 300);
m.put("4", 150);
m.put("5", 110);
m.put("6", 50);
m.put("7", 90);
m.put("8", 250);
m.put("9", 350);
m.put("10", 450);现在,使用以下方法检查该 Map 是否为空。由于我们上面添加了一些元素,因此该 Map 不为空
n.isEmpty();
示例
import java.util.*;
public class Demo {
public static void main(String[] args) {
Map<String, Integer> m = new IdentityHashMap<String, Integer>();
m.put("1", 100);
m.put("2", 200);
m.put("3", 300);
m.put("4", 150);
m.put("5", 110);
m.put("6", 50);
m.put("7", 90);
m.put("8", 250);
m.put("9", 350);
m.put("10", 450);
System.out.println("Map elements = "+ m);
System.out.println("Is the map empty? "+ m.isEmpty());
}
}输出
以下为输出
Map elements = {2=200, 4=150, 9=350, 7=90, 10=450, 6=50, 5=110, 8=250, 1=100, 3=300}
Is the map empty? false
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP