Monday, November 18, 2024
Google search engine
HomeLanguagesJavaConcurrentHashMap get() Method in Java

ConcurrentHashMap get() Method in Java

The get() method in Java’s ConcurrentHashMap class is used to retrieve the value associated with a given key. It has the following signature:

V get(Object key)

where:

key is the key whose associated value is to be retrieved.
The get() 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 get() operation is performed, the method first acquires the lock on the segment of the map corresponding to the given key and then retrieves the value associated with the key. If the key is not present in the map, the method returns null.

Here is an example of using the get() 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);
 
        Integer result = map.get("Bob");
        System.out.println("Value associated with key Bob: "
                           + result); // 30
 
        result = map.get("Dave");
        System.out.println(
            "Value associated with key Dave: "
            + result); // null
    }
}


Output

Value associated with key Bob: 30
Value associated with key Dave: null

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

Next, we perform a get() operation to retrieve the value associated with the key “Bob”. The method returns 30, and we print out the result.

Finally, we perform a get() operation to retrieve the value associated with the key “Dave”. The method returns null, and we print out the result.

The get() method of java.util.concurrent.ConcurrentHashMap is an in-built function in Java which accepts a key as parameter and returns the value mapped to it. It returns null if no mapping exists for the key passed as parameter. 

Syntax:

Concurrent.get(Object key_element)

Parameters: The method accepts a single parameter key_element of object type which refers to the key whose associated value is supposed to be returned. 

Return Value: The method returns the value associated with the key_element in the parameter. 

Exception: The function throws NullPointerException when the specified key_element is null. 

The below programs illustrate the use of java.util.concurrent.ConcurrentHashMap.get() method: 

Program 1: This program involves mapping String Values to Integer Keys. 

Java




// Java Program Demonstrate get()
// 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");
        chm.put(103, "Contribute");
 
        // Displaying the HashMap
        System.out.println("The Mappings are: ");
        System.out.println(chm);
 
        // Display the value of 100
        System.out.println("The Value associated to "
                           + "100 is : " + chm.get(100));
 
        // Getting the value of 103
        System.out.println("The Value associated to "
                           + "103 is : " + chm.get(103));
    }
}


Output

The Mappings are: 
{100=Geeks, 101=for, 102=Geeks, 103=Contribute}
The Value associated to 100 is : Geeks
The Value associated to 103 is : Contribute

Program 2: This program involves mapping Integer Values to String Keys. 

Java




// Java Program Demonstrate get()
// method of ConcurrentHashMap
 
import java.util.concurrent.*;
 
class GFG {
    public static void main(String[] args)
    {
 
        ConcurrentHashMap<String, Integer> chm
            = new ConcurrentHashMap<String, Integer>();
 
        chm.put("Geeks", 100);
        chm.put("GFG", 10);
        chm.put("Lazyroar", 25);
        chm.put("Contribute", 102);
 
        // Displaying the HashMap
        System.out.println("The Mappings are: ");
        System.out.println(chm);
 
        // Display the value of Geeks
        System.out.println("The Value associated to "
                           + "Geeks is : "
                           + chm.get("Geeks"));
 
        // Getting the value of Contribute
        System.out.println("The Value associated to "
                           + "Contribute is : "
                           + chm.get("Contribute"));
    }
}


The Mappings are: 
{Lazyroar=25, Geeks=100, GFG=10, Contribute=102}
The Value associated to Geeks is : 100
The Value associated to Contribute is : 102

RELATED ARTICLES

Most Popular

Recent Comments