ConcurrentHashMap 類的 mappingCount() 方法返回映射數。返回的值是一個估計值;如果存在並發插入或刪除,實際計數可能會有所不同。
用法
public long mappingCount()
參數
沒有傳遞參數。
返回
映射數
拋出
不會拋出任何異常。
例子1
import java.util.concurrent.ConcurrentHashMap;
import java.util.*;
public class ConcurrentHashMapmappingCountExample1 {
public static void main(String[] args)
{
ConcurrentHashMap<String, String> chm = new ConcurrentHashMap<String, String>();
chm.put("1", "1");
chm.put("2", "10");
chm.put("3", "100");
chm.put("4", "2");
chm.put("5", "20");
chm.put("6", "200");
// print statement.
System.out.println("size of map before clearing:" + chm.mappingCount());
chm.clear(); // clear will clear all the values in map.
System.out.println("Map after calling clear():"+ chm);
System.out.println("size of map after clearing:" + chm.mappingCount());
}
}
輸出:
size of map before clearing:6 Map after calling clear():{} size of map after clearing:0
例子2
import java.util.*;
import java.util.concurrent.*;
public class ConcurrentHashMapmappingCountExample2 {
public static void main(String[] args)
{
ConcurrentHashMap<Integer, String> conmap = new ConcurrentHashMap<Integer, String>();
conmap.put(10, "Java");
conmap.put(11, ".net");
conmap.put(20, "php");
conmap.put(12, "C");
conmap.put(30, "C++");
System.out.println(" Map:" + conmap);
System.out.println("keySet:" + conmap.keySet());
System.out.println("Map Size :" + conmap.mappingCount());
}
}
輸出:
Map:{20=php, 10=Java, 11=.net, 12=C, 30=C++} keySet:[20, 10, 11, 12, 30] Map Size :5
相關用法
- Java ConcurrentHashMap merge()用法及代碼示例
- Java ConcurrentHashMap contains()用法及代碼示例
- Java ConcurrentHashMap newKeySet()用法及代碼示例
- Java ConcurrentHashMap elements()用法及代碼示例
- Java ConcurrentHashMap forEach()用法及代碼示例
- Java ConcurrentHashMap putIfAbsent()用法及代碼示例
- Java ConcurrentHashMap computeIfAbsent()用法及代碼示例
- Java ConcurrentHashMap isEmpty()用法及代碼示例
- Java ConcurrentHashMap equals()用法及代碼示例
- Java ConcurrentHashMap putAll()用法及代碼示例
- Java ConcurrentHashMap hashcode()用法及代碼示例
- Java ConcurrentHashMap containsKey()用法及代碼示例
- Java ConcurrentHashMap put()用法及代碼示例
- Java ConcurrentHashMap computeIfPresent()用法及代碼示例
- Java ConcurrentHashMap toString()用法及代碼示例
- Java ConcurrentHashMap replace()用法及代碼示例
- Java ConcurrentHashMap clear()用法及代碼示例
- Java ConcurrentHashMap size()用法及代碼示例
- Java ConcurrentHashMap remove()用法及代碼示例
- Java ConcurrentHashMap keys()用法及代碼示例
注:本文由純淨天空篩選整理自 Java ConcurrentHashMap mappingCount() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。