检查IdentityHashMap在Java中是否为空?
使用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热门推荐
10 店家搬家祝福语大全简短
11 银行考研上岸祝福语简短
12 团队海报祝福语简短英语
13 老师开工仪式祝福语简短
14 文艺结婚祝福语短语简短
15 夸赞老师祝福语搞笑简短
16 春季祝福语女生文案简短
17 开店大吉文案祝福语简短
18 六一互换礼物祝福语简短