ConcurrentHashMap size() Method in Java
The java.util.concurrent.ConcurrentHashMap.size() method is an in-built function in Java which counts the number of key-value mappings in this map and returns the integer value.
Syntax:
public int size()
Return Value: The function returns an integer value which denotes the number of key-value mappings in this map.
Below program illustrate the use of size() method :
Program 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()); } } |
Output:
The number of key-value mappings is 3
Program 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()); } } |
Output:
The number of key-value mappings is 6
Reference : https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html#size()