Java ConcurrentHashMap size() Method

The ConcurrentHashMap.size() method in Java is used to get the number of key-value pairs in a ConcurrentHashMap.

Table of Contents

  1. Introduction
  2. size Method Syntax
  3. Examples
    • Getting the Size of a ConcurrentHashMap
    • After Adding and Removing Entries
  4. Real-World Use Case
    • Example: Monitoring Active User Sessions
  5. Conclusion

Introduction

The ConcurrentHashMap.size() method is a member of the ConcurrentHashMap class in Java. It allows you to determine the number of key-value pairs present in the map. The ConcurrentHashMap class is part of the java.util.concurrent package, designed for high concurrency and scalability.

size() Method Syntax

The syntax for the size method is as follows:

public int size()
  • The method takes no parameters.
  • The method returns an integer representing the number of key-value pairs in the map.

Examples

Getting the Size of a ConcurrentHashMap

The size method can be used to get the number of key-value pairs in a ConcurrentHashMap.

Example

import java.util.concurrent.ConcurrentHashMap;

public class SizeExample {
    public static void main(String[] args) {
        // Creating a ConcurrentHashMap with String keys and Integer values
        ConcurrentHashMap<String, Integer> people = new ConcurrentHashMap<>();

        // Adding entries to the ConcurrentHashMap
        people.put("Ravi", 25);
        people.put("Priya", 30);
        people.put("Vijay", 35);

        // Getting the size of the ConcurrentHashMap
        int size = people.size();

        // Printing the size
        System.out.println("Size of ConcurrentHashMap: " + size);
    }
}

Output:

Size of ConcurrentHashMap: 3

After Adding and Removing Entries

The size method reflects changes in the map after adding or removing entries.

Example

import java.util.concurrent.ConcurrentHashMap;

public class AddRemoveSizeExample {
    public static void main(String[] args) {
        // Creating a ConcurrentHashMap with String keys and Integer values
        ConcurrentHashMap<String, Integer> people = new ConcurrentHashMap<>();

        // Adding entries to the ConcurrentHashMap
        people.put("Ravi", 25);
        people.put("Priya", 30);
        people.put("Vijay", 35);

        // Printing the initial size
        System.out.println("Initial size: " + people.size());

        // Removing an entry
        people.remove("Priya");

        // Printing the size after removal
        System.out.println("Size after removal: " + people.size());

        // Adding a new entry
        people.put("Anita", 28);

        // Printing the final size
        System.out.println("Final size: " + people.size());
    }
}

Output:

Initial size: 3
Size after removal: 2
Final size: 3

Real-World Use Case

Example: Monitoring Active User Sessions

A common real-world use case for ConcurrentHashMap is managing and monitoring active user sessions.

Example

import java.util.concurrent.ConcurrentHashMap;

public class UserSessionStore {
    public static void main(String[] args) {
        // Creating a ConcurrentHashMap to manage user sessions
        ConcurrentHashMap<String, String> userSessions = new ConcurrentHashMap<>();

        // Adding user sessions to the ConcurrentHashMap
        userSessions.put("Ravi", "Active");
        userSessions.put("Priya", "Inactive");
        userSessions.put("Vijay", "Active");
        userSessions.put("Anita", "Active");

        // Getting the size of the user sessions
        int activeSessions = userSessions.size();

        // Printing the number of active sessions
        System.out.println("Number of user sessions: " + activeSessions);
    }
}

Output:

Number of user sessions: 4

In this example, ConcurrentHashMap is used to manage user session data, and the size method is employed to monitor the number of active user sessions in a thread-safe manner.

Conclusion

The ConcurrentHashMap.size() method in Java provides a way to get the number of key-value pairs in a ConcurrentHashMap in a thread-safe manner. By understanding how to use this method, you can efficiently manage collections of key-value pairs in your Java applications, especially in concurrent environments. The method allows you to monitor the size of the map, making it a versatile tool for data management in multi-threaded scenarios.

Comments