Java ConcurrentHashMap containsValue() Method

The ConcurrentHashMap.containsValue() method in Java is used to check if a specified value is present in a ConcurrentHashMap.

Table of Contents

  1. Introduction
  2. containsValue Method Syntax
  3. Examples
    • Checking for Values in a ConcurrentHashMap
    • Handling Non-Existent Values
  4. Real-World Use Case
    • Example: Verifying User Session States
  5. Conclusion

Introduction

The ConcurrentHashMap.containsValue() method is a member of the ConcurrentHashMap class in Java. It allows you to check if a specific value is present in the map. The ConcurrentHashMap class is part of the java.util.concurrent package, designed for high concurrency and scalability.

containsValue() Method Syntax

The syntax for the containsValue method is as follows:

public boolean containsValue(Object value)
  • The method takes one parameter:
    • value of type Object, which represents the value to be checked for presence in the map.
  • The method returns true if the value is present in the map, and false otherwise.

Examples

Checking for Values in a ConcurrentHashMap

The containsValue method can be used to check for the presence of values in a ConcurrentHashMap.

Example

import java.util.concurrent.ConcurrentHashMap;

public class ContainsValueExample {
    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);

        // Checking for the presence of values
        boolean hasAge25 = people.containsValue(25);
        boolean hasAge40 = people.containsValue(40);

        // Printing the results
        System.out.println("Contains value 25: " + hasAge25);
        System.out.println("Contains value 40: " + hasAge40);
    }
}

Output:

Contains value 25: true
Contains value 40: false

Handling Non-Existent Values

The containsValue method returns false if the specified value is not present in the ConcurrentHashMap.

Example

import java.util.concurrent.ConcurrentHashMap;

public class NonExistentValueExample {
    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);

        // Checking for the presence of a non-existent value
        boolean hasAge35 = people.containsValue(35);

        // Printing the result
        System.out.println("Contains value 35: " + hasAge35);
    }
}

Output:

Contains value 35: false

Real-World Use Case

Example: Verifying User Session States

A common real-world use case for ConcurrentHashMap is managing user session states and verifying if a specific session state exists.

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");

        // Checking for the presence of session states
        boolean isActiveSession = userSessions.containsValue("Active");
        boolean isPendingSession = userSessions.containsValue("Pending");

        // Printing the results
        System.out.println("Contains 'Active' session: " + isActiveSession);
        System.out.println("Contains 'Pending' session: " + isPendingSession);
    }
}

Output:

Contains 'Active' session: true
Contains 'Pending' session: false

In this example, ConcurrentHashMap is used to manage user session states, and the containsValue method is employed to verify the existence of specific session states in a thread-safe manner.

Conclusion

The ConcurrentHashMap.containsValue() method in Java provides a way to check for the presence of specific values 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 handle the presence and absence of values, making it a versatile tool for data management in multi-threaded scenarios.

Comments