📜  Java中的 ConcurrentHashMap size() 方法

📅  最后修改于: 2022-05-13 01:54:56.198000             🧑  作者: Mango

Java中的 ConcurrentHashMap size() 方法

Java .util.concurrent.ConcurrentHashMap.size()方法是Java中的一个内置函数,它计算此映射中键值映射的数量并返回整数值。

句法:

public int size()

返回值:该函数返回一个数值,表示此映射中键值映射的数量。

下面的程序说明了size()方法的使用:

方案一:

// Java Program Demonstrate size()
// method of ConcurrentHashMap
  
import java.util.concurrent.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        ConcurrentHashMap
            chm = new ConcurrentHashMap();
  
        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

方案二:

// Java Program Demonstrate size()
// method of ConcurrentHashMap
  
import java.util.concurrent.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        ConcurrentHashMap
            chm = new ConcurrentHashMap();
  
        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: Java/util/concurrent/ConcurrentHashMap.html#size()