What is AbstractMap get() in Java?

AbstractMap.get() is a method of the Java AbstractMap class. We use it to return the value a specified key maps to in the AbstractMap maps, or to return NULL if the AbstractMap contains no mapping for the specified key.

Syntax

AbstractMap.get(Object key);

The AbstractMap.get() method takes one parameter, keyof type Object, mapped to the supposed return value.

AbstractMapDemo.java illustration
AbstractMapDemo.java illustration

Code

We can write the illustration shown above in the code snippet below.

import java.util.*;
class AbstractMapDemo {
public static void main(String[] args)
{
// Creating an AbstractMap
AbstractMap<Integer, String>
abstractMap = new HashMap<Integer, String>();
// Mapping string values to int keys
abstractMap.put(21, "Shot");
abstractMap.put(42, "Java");
abstractMap.put(34, "Yaaaayy");
// Printing the AbstractMap
System.out.println("The Mappings are: "
+ abstractMap);
// Getting the value of key 21
System.out.println("The value of key 21 is: "
+ abstractMap.get(21));
// Getting the value of key 35
System.out.println("The value of key 34 is: "
+ abstractMap.get(35));
}
}
New on Educative
Learn any Language for FREE all September 🎉
For the entire month of September, get unlimited access to our entire catalog of beginner coding resources.
🎁 G i v e a w a y
30 Days of Code
Complete Educative’s daily coding challenge every day in September, and win exciting Prizes.

Free Resources