当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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()



相关用法


注:本文由纯净天空筛选整理自RICHIK BHATTACHARJEE大神的英文原创作品 ConcurrentHashMap size() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。