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.
// 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)); } } |
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 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")); } } |
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()
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- ConcurrentHashMap put() method in Java
- ConcurrentHashMap remove() method in Java
- ConcurrentHashMap containsValue() Method in Java
- ConcurrentHashMap containsKey() Method in Java
- ConcurrentHashMap size() Method in Java
- ConcurrentHashMap isEmpty() Method in Java
- ConcurrentHashMap putAll() method in Java
- ConcurrentHashMap putIfAbsent() Method in Java
- ConcurrentHashMap keys() method in Java with Examples
- ConcurrentHashMap elements() method in Java with Examples
- ConcurrentHashMap computeIfAbsent() method in Java with Examples
- ConcurrentHashMap compute() method in Java with Examples
- ConcurrentHashMap entrySet() method in Java with Examples
- ConcurrentHashMap contains() method in Java with Examples
- ConcurrentHashMap values() method in Java with Examples
- ConcurrentHashMap keySet() method in Java with Examples
- ConcurrentHashMap in Java
- Java ConcurrentHashMap | clear()
- Difference between HashMap and ConcurrentHashMap
- Get Credential Information From the URL(GET Method) in Java
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.

