In Java, get(key) returns the value that a TreeMap stores under key, or null if there is no such key. The map does not change.
The lookup walks down the red-black tree, comparing key with the keys it meets, so it takes O(log n) time. A key is found when the comparison returns 0; with a case-insensitive comparator, "FRANCE" finds the entry stored as "France".
An instance method of java.util.TreeMap, part of TreeMap since Java 1.2. A TreeMap keeps its key/value pairs sorted by key in a red-black tree, so lookups and updates take O(log n) time; it is not thread-safe.
Syntax
Pass the key; you get the value or null.
Java
TreeMap<String, String> glossary = new TreeMap<>();
glossary.put("nibble", "4 bits");
String a = glossary.get("nibble"); // "4 bits"
String b = glossary.get("word"); // null
Call
What it does
map.get(k)
The value for k, or null.
map.getOrDefault(k, d)
The value, or d when k is missing.
map.containsKey(k)
Tells "missing" apart from "mapped to null".
The parameter type is Object, so get(42) on a TreeMap<String, ...> compiles, but it throws ClassCastException when the map compares 42 with its String keys.
Library declarations
Java
// java.util.TreeMap<K, V>
public V get(Object key)
// Declared in Map<K, V>
Parameters
Parameter
Description
key (Object)
The key to look up. Not null under natural ordering.
Return Value
Return Value
Description
V
The value mapped to a key that compares equal to key.
Hash lookup, usually constant time; allows a null key.
Java Examples
Example 1 — Look Up Words in a Sorted Glossary
Read one definition, try a word that is missing, and react to the null.
Java
import java.util.TreeMap;
public class TreeMapGetGlossary {
public static void main(String[] args) {
TreeMap<String, String> glossary = new TreeMap<>();
glossary.put("byte", "8 bits");
glossary.put("bit", "a single 0 or 1");
glossary.put("nibble", "4 bits");
System.out.println("nibble = " + glossary.get("nibble"));
System.out.println("word = " + glossary.get("word"));
String meaning = glossary.get("kilobyte");
if (meaning == null) {
System.out.println("kilobyte is not in the glossary yet");
}
}
}
Output
nibble = 4 bits
word = null
kilobyte is not in the glossary yet
How It Works
Exact hit. The search for "nibble" compares it with the keys on its path through the tree and stops where the comparison returns 0, giving "4 bits".
A miss is null, not an error. "word" is not a key, so get returns null, and string concatenation prints the word null.
Check before use. Testing meaning == null is the usual way to handle a missing entry when the map never stores null values.
Example 2 — Find Capitals Without Worrying About Case
The same data, once in a case-insensitive map and once in a naturally ordered copy.
Java
import java.util.TreeMap;
public class TreeMapGetIgnoreCase {
public static void main(String[] args) {
TreeMap<String, String> capitals = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
capitals.put("France", "Paris");
capitals.put("Japan", "Tokyo");
TreeMap<String, String> strict = new TreeMap<>();
strict.putAll(capitals);
System.out.println("FRANCE (ignore case): " + capitals.get("FRANCE"));
System.out.println("japan (ignore case): " + capitals.get("japan"));
System.out.println("FRANCE (natural order): " + strict.get("FRANCE"));
}
}
Output
FRANCE (ignore case): Paris
japan (ignore case): Tokyo
FRANCE (natural order): null
How It Works
Lookup uses the comparator. In capitals, "FRANCE" compares equal to "France", so get returns Paris even though "FRANCE".equals("France") is false.
Any spelling works. "japan" in lowercase finds Japan for the same reason.
Natural order is strict.strict was created without a comparator, so "FRANCE" and "France" are different keys there and the lookup returns null.
Example 3 — Missing Key, null Value, and null Key
Locker B07 has no code yet (null), C99 does not exist, and get(null) is not allowed.
Java
import java.util.TreeMap;
public class TreeMapGetNullCases {
public static void main(String[] args) {
TreeMap<String, Integer> lockerCodes = new TreeMap<>();
lockerCodes.put("A12", 4071);
lockerCodes.put("B07", null);
System.out.println("B07: " + lockerCodes.get("B07") + ", has key: " + lockerCodes.containsKey("B07"));
System.out.println("C99: " + lockerCodes.get("C99") + ", has key: " + lockerCodes.containsKey("C99"));
try {
lockerCodes.get(null);
} catch (NullPointerException e) {
System.out.println("get(null): " + e.getClass().getSimpleName());
}
}
}
Output
B07: null, has key: true
C99: null, has key: false
get(null): NullPointerException
How It Works
Same result, different meaning. Both B07 and C99 give null from get; only containsKey reveals that B07 exists with a null value.
The key itself cannot be null. With natural ordering the map cannot compare null, so get(null) throws instead of returning null.
Unboxing trap avoided. The results are printed as Integer; writing int code = lockerCodes.get("B07"); would throw NullPointerException.
Frequently Asked Questions
null. The map is not changed and no exception is thrown (unless the key itself is null or of the wrong type).
O(log n). The tree is searched from the root, comparing keys on the way down.
TreeMap matches keys with its comparator, not with equals. A case-insensitive comparator makes "FRANCE" and "France" the same key.
Not with natural ordering: it throws NullPointerException. Only a comparator that accepts null allows it.
Call containsKey(key). It returns true for a key mapped to null and false for a missing key.