Java ConcurrentHashMap size()方法
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()