當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。