Open In App

ConcurrentHashMap size() Method in Java

Last Updated : 20 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The size() method in Java’s ConcurrentHashMap class is used to retrieve the number of key-value mappings in the map. It has the following signature: 

int size()

The size() method works in a concurrent environment, which means that it can be called from multiple threads without causing any data race or synchronization issues.

When a size() operation is performed, the method first acquires the lock on all the segments of the map and then calculates the total number of key-value mappings in the map.

Here is an example of using the size() method:

Java




import java.util.concurrent.ConcurrentHashMap;
 
public class ConcurrentHashMapExample {
    public static void main(String[] args) {
        ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
        map.put("Alice", 25);
        map.put("Bob", 30);
        map.put("Charlie", 35);
 
        int size = map.size();
        System.out.println("Size of the map: " + size); // 3
    }
}


Output

Size of the map: 3

In this example, we first create a ConcurrentHashMap and add three key-value pairs to it using the put() method.

Next, we perform a size() operation to retrieve the number of key-value mappings in the map. The method returns 3, and we print out the result.

The java.util.concurrent.ConcurrentHashMap.size() method is an in-built function in Java that 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 that denotes the number of key-value mappings in this map. Below program illustrates the use of the size() method: 

Program 1: 

Java




// 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




// 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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads