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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


Output:

The number of key-value mappings is 3

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


Output:

The number of key-value mappings is 6

Reference : https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html#size()



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.