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()); } } |
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()); } } |
The number of key-value mappings is 6
Reference : https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html#size()
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

