java.util.concurrent.ConcurrentHashMap.size()方法是Java中的內置函數,該函數對映射中鍵-值映射的數目進行計數並返回整數值。
用法:
public int size()
返回值:該函數返回一個整數值,該整數值表示此映射中的鍵-值映射數。
以下示例程序旨在說明size()方法的使用:
示例1:
// Java Program Demonstrate size()
// method of ConcurrentHashMap
import java.util.concurrent.*;
class GFG {
public static void main(String[] args)
{
ConcurrentHashMap<Integer, String>
chm = new ConcurrentHashMap<Integer, String>();
chm.put(100, "Geeks");
chm.put(101, "for");
chm.put(102, "Geeks");
// Display the number of
// key-value mappings
System.out.println("The number of "
+ "key-value mappings is "
+ chm.size());
}
}
輸出:
The number of key-value mappings is 3
示例2:
// Java Program Demonstrate size()
// method of ConcurrentHashMap
import java.util.concurrent.*;
class GFG {
public static void main(String[] args)
{
ConcurrentHashMap<Integer, Integer>
chm = new ConcurrentHashMap<Integer, Integer>();
chm.put(1, 100);
chm.put(2, 200);
chm.put(3, 300);
chm.put(4, 400);
chm.put(5, 500);
chm.put(6, 600);
// Display the number of
// key-value mappings
System.out.println("The number of "
+ "key-value mappings is "
+ chm.size());
}
}
輸出:
The number of key-value mappings is 6
參考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html#size()
相關用法
- Java ConcurrentHashMap get()用法及代碼示例
- Java ConcurrentHashMap put()用法及代碼示例
- Java ConcurrentHashMap putIfAbsent()用法及代碼示例
- Java ConcurrentHashMap isEmpty()用法及代碼示例
- Java ConcurrentHashMap containsValue()用法及代碼示例
- Java ConcurrentHashMap contains()用法及代碼示例
- Java ConcurrentHashMap containsKey()用法及代碼示例
- Java ConcurrentHashMap putAll()用法及代碼示例
- Java ConcurrentHashMap remove()用法及代碼示例
- Java ConcurrentHashMap compute()用法及代碼示例
- Java ConcurrentHashMap values()用法及代碼示例
- Java ConcurrentHashMap entrySet()用法及代碼示例
- Java ConcurrentHashMap keySet()用法及代碼示例
- Java ConcurrentHashMap elements()用法及代碼示例
- Java ConcurrentHashMap computeIfAbsent()用法及代碼示例
注:本文由純淨天空篩選整理自RICHIK BHATTACHARJEE大神的英文原創作品 ConcurrentHashMap size() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。