How to Get a Value from a Map in Java with get
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
ValueType value = map.get(key);
| Part | What it does |
|---|---|
map.get(key) | Returns the value stored under key, or null when the map has no such key. |
key | The key to look up. It has to equal the stored key exactly, so "Pro" doesn't find "pro". |
ValueType | The 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
.javafile directly. This guide uses OpenJDK 25.0.5.1.
Step 1: Write the Code
Create a file named 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
java Main.java
500
null

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 underpro.limits.get("team")returnednull. The map has no keyteam, andgetdoesn't treat that as an error.printlnprinted thenullas the wordnull.
Common Mistakes
| Mistake | What happens | Do 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 int | Use getOrDefault, or keep the value in an Integer and check it for null |
| Looking a key up with other capital letters | get("Pro") returns null for a key stored as pro | Use the key exactly as it's stored |
Looking a key up with the wrong type, as in get(10) on a map with String keys | It compiles, and returns null | Pass a key of the map's key type |
get(null) on a map made with Map.of | NullPointerException: Cannot invoke "Object.equals(Object)" because "o" is null. A HashMap returns null instead | Check the key for null before the lookup |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because the return value of "java.util.Map.get(Object)" is null | get returned null for a missing key, and the code needed an int | Use getOrDefault with a default value |
get returns null for a key you can see when you print the map | The key you pass differs in capital letters or in type | Compare it with the printed keys |
NullPointerException: Cannot invoke "Object.equals(Object)" because "o" is null, at ImmutableCollections$Map1.get | get(null) on a map made with Map.of | Check 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
- get, getOrDefault and containsKey, in the Java 25 API documentation for
Map.
