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 to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources