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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。