ConcurrentHashMap get() Method in Java

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.

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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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("GeeksforGeeks", 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"));
    }
}

chevron_right


Output:

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

Reference : https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html#get()



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.