檢查 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

更新日期: 2020 年 6 月 29 日

128 次瀏覽

開啟你的 事業

完成課程,獲取認證

開始學習
廣告
© . All rights reserved.