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