獲取 Java 中 IdentityHashMap 的大小


要獲取 IdentityHashMap 的大小,請使用 size() 方法。此方法返回 IdentityHashMap 中元素的計數。

我們首先建立一個 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);

現在,獲取其大小

m.size();

下面是一個獲取 IdentityHashMap 大小的示例

示例

 即時演示

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      Map<String, Integer> m = newIdentityHashMap<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("IdentityHashMap elements
"+ m); System.out.println("Size = " + m.size()); } }

輸出如下

IdentityHashMap elements
{2=200, 4=150, 9=350, 7=90, 10=450, 6=50, 5=110, 8=250, 1=100, 3=300}
Size = 10

更新於: 30-7-2019

97 次瀏覽

開啟你的 職業

完成課程後可以獲得認證資格

開始
廣告
© . All rights reserved.