Java ConcurrentHashMap values() Method

The ConcurrentHashMap.values() method in Java is used to obtain a collection view of the values contained in the ConcurrentHashMap.

Table of Contents

  1. Introduction
  2. values Method Syntax
  3. Examples
    • Retrieving Values from a ConcurrentHashMap
    • Iterating Over Values
  4. Real-World Use Case
    • Example: Listing Active User Ages
  5. Conclusion

Introduction

The ConcurrentHashMap.values() method is a member of the ConcurrentHashMap class in Java. It provides a collection view of the values contained in the map. The ConcurrentHashMap class is part of the java.util.concurrent package, designed for high concurrency and scalability.

values() Method Syntax

The syntax for the values method is as follows:

public Collection<V> values()
  • The method takes no parameters.
  • The method returns a Collection view of the values contained in the map.

Examples

Retrieving Values from a ConcurrentHashMap

The values method can be used to retrieve the values from a ConcurrentHashMap.

Example

import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;

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

        // Retrieving the collection of values
        Collection<Integer> values = people.values();

        // Printing the collection of values
        System.out.println("Values in ConcurrentHashMap: " + values);
    }
}

Output:

Values in ConcurrentHashMap: [25, 30, 35]

Iterating Over Values

You can iterate over the values obtained from the values method.

Example

import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;

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

        // Retrieving the collection of values
        Collection<Integer> values = people.values();

        // Iterating over the values
        System.out.println("Iterating over values:");
        for (Integer value : values) {
            System.out.println(value);
        }
    }
}

Output:

Iterating over values:
25
30
35

Real-World Use Case

Example: Listing Active User Ages

A common real-world use case for ConcurrentHashMap is managing user session data and listing ages of active users.

Example

import java.util.Collection;
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<>();
        ConcurrentHashMap<String, Integer> userAges = new ConcurrentHashMap<>();

        // Adding user sessions and ages to the ConcurrentHashMap
        userSessions.put("Ravi", "Active");
        userAges.put("Ravi", 25);
        userSessions.put("Priya", "Inactive");
        userAges.put("Priya", 30);
        userSessions.put("Vijay", "Active");
        userAges.put("Vijay", 35);

        // Retrieving the collection of values
        Collection<Integer> activeUserAges = userAges.values();

        // Printing the ages of active users
        System.out.println("Ages of Active Users:");
        for (String key : userSessions.keySet()) {
            if ("Active".equals(userSessions.get(key))) {
                System.out.println(userAges.get(key));
            }
        }
    }
}

Output:

Ages of Active Users:
25
35

In this example, ConcurrentHashMap is used to manage user session data and their corresponding ages. The values method is employed to list the ages of active users in a thread-safe manner.

Conclusion

The ConcurrentHashMap.values() method in Java provides a way to obtain a collection view of the values contained 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 retrieve and iterate over values, making it a versatile tool for data management in multi-threaded scenarios.

Comments