Skip to main content

How to Get a Value from a Map in Java with get

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

map.get(key) returns the value a Java map stores under a key. When the map has no such key, get returns null instead of throwing an error, so the code that uses the value has to be ready for null.

Syntax​

Syntax
ValueType value = map.get(key);
PartWhat it does
map.get(key)Returns the value stored under key, or null when the map has no such key.
keyThe key to look up. It has to equal the stored key exactly, so "Pro" doesn't find "pro".
ValueTypeThe type of the map's values, such as Integer for a Map<String, Integer>.

The Map documentation: "Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key."

The Example​

The example maps two plans to their limits, then gets the limit of pro, which the map has, and of team, which it doesn't.

Prerequisites​

  • A JDK 11 or later, which can run a single .java file directly. This guide uses OpenJDK 25.0.5.1.

Step 1: Write the Code​

Create a file named Main.java:

Main.java, which creates a map of free 10 and pro 500 with Map.of, then prints the value for pro and the value for team, a key the map doesn't have.

Main.java
import java.util.Map;

public class Main {

public static void main(String[] args) {
Map<String, Integer> limits = Map.of("free", 10, "pro", 500);

System.out.println(limits.get("pro"));
System.out.println(limits.get("team"));
}
}

Step 2: Run It​

Terminal
java Main.java
Output
500
null

A zsh terminal where java Main.java prints 500, then null for the key the map doesn't have.

java Main.java compiles the file in memory and runs it. The command is the same in zsh, bash and PowerShell.

What Happened​

  • limits.get("pro") returned 500, the value stored under pro.
  • limits.get("team") returned null. The map has no key team, and get doesn't treat that as an error. println printed the null as the word null.

Common Mistakes​

MistakeWhat happensDo this instead
Storing what get returns in an int, as in int limit = limits.get("team");For a missing key, the program stops with a NullPointerException, because Java can't turn null into an intUse getOrDefault, or keep the value in an Integer and check it for null
Looking a key up with other capital lettersget("Pro") returns null for a key stored as proUse the key exactly as it's stored
Looking a key up with the wrong type, as in get(10) on a map with String keysIt compiles, and returns nullPass a key of the map's key type
get(null) on a map made with Map.ofNullPointerException: Cannot invoke "Object.equals(Object)" because "o" is null. A HashMap returns null insteadCheck the key for null before the lookup

Troubleshooting​

SymptomLikely causeFix
NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because the return value of "java.util.Map.get(Object)" is nullget returned null for a missing key, and the code needed an intUse getOrDefault with a default value
get returns null for a key you can see when you print the mapThe key you pass differs in capital letters or in typeCompare it with the printed keys
NullPointerException: Cannot invoke "Object.equals(Object)" because "o" is null, at ImmutableCollections$Map1.getget(null) on a map made with Map.ofCheck the key for null first

FAQ​

How do I get a default value instead of null?​

Use getOrDefault(key, defaultValue). Its documentation: "Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key." On the map from this example, limits.getOrDefault("team", 0) returned 0, and limits.getOrDefault("pro", 0) returned 500.

How do I check whether a key exists?​

Use containsKey: limits.containsKey("pro") returned true.

Why does get accept a key of the wrong type?​

get takes any Object as its key, as its signature get(Object key) shows. So get(10) compiles on a map with String keys, and finds nothing.

Conclusion​

map.get(key) returns the value stored under a key, and null when there's none. Handle the null before using the value, or use getOrDefault to get a value of your choice instead.

Additional Resources​