CKTIS / Java
Map.get — look up a value by key
Use `map.get(key)` to retrieve a stored value. If the map allows `null` values, the result alone cannot distinguish a missing key from one explicitly mapped to `null`.
Version: Java SE 21
Syntax
V get(Object key); // instance method declared by java.util.Map<K, V>
map.get(key) // call with one key argumentIn Java SE 21, Map.get is an instance method of java.util.Map<K, V> declared as V get(Object key). Map.get() is shorthand for the method name, not a zero-argument overload: call map.get(key). It returns the stored value, or null if there is no mapping. The Map contract describes a match as Objects.equals(key, k), where k is the stored key. In a HashMap, a different but equal key instance can find the same entry. Keys that are equal under equals must have the same hashCode; do not change the properties that affect a key’s equality while it is in the map.
With Map<Integer, Long>, Long total = totals.get(7); retrieves a previously computed value without a cast; the parameter type is Object, not K. A raw Map (without type arguments) returns Object. To assign that result to String or Integer, use an explicit cast, such as (String) raw.get(key) or (Integer) raw.get(key). Casting an incompatible non-null value throws ClassCastException. Prefer maps with type arguments.
If the map permits null values, get cannot distinguish a missing key from one mapped to null. Use containsKey(key) to check presence; the two calls are not one atomic operation if the map is changing concurrently. getOrDefault(key, fallback) uses fallback only when there is no mapping. Depending on the implementation, an unsuitable key type may cause ClassCastException, and a disallowed null key may cause NullPointerException. Read stored true and false values by their specific keys instead of relying on iteration order. Unboxing a null result, for example from Boolean to boolean, throws NullPointerException.
In an access-ordered LinkedHashMap (accessOrder = true in the constructor), a hit moves the entry to the end of iteration order, even when its value is null; a miss leaves the order unchanged. In insertion-order mode, get does not change the order.
When multiple threads write results, use a map such as ConcurrentHashMap, which disallows null keys and values. After the main thread successfully returns from join() on each worker, the workers’ writes are visible to it. Look up results by ascending request index to get request order rather than completion order. Joining does not make earlier concurrent writes to an ordinary HashMap safe.
Examples
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class MapGetDemo {
record Key(int id) {}
public static void main(String[] args) {
Map<Key, Long> totals = new HashMap<>();
totals.put(new Key(7), 6L * 7);
totals.put(new Key(8), null);
Long total = totals.get(new Key(7));
System.out.println("total: " + total);
for (int id : new int[] {8, 9}) {
Key key = new Key(id);
System.out.println(id + ": " + totals.get(key) + ", present: " + totals.containsKey(key));
}
Map<String, Boolean> flags = Map.of("a", true, "b", false);
System.out.println("flags: " + flags.get("a") + ", " + flags.get("b"));
Map<String, Integer> recent = new LinkedHashMap<>(16, 0.75f, true);
recent.put("a", 1);
recent.put("b", 2);
System.out.println("before: " + recent.keySet());
System.out.println("get a: " + recent.get("a"));
System.out.println("after: " + recent.keySet());
}
}Result
total: 42 8: null, present: true 9: null, present: false flags: true, false before: [a, b] get a: 1 after: [b, a]
A new `Key(7)` finds the stored value `42`. Both `8` and `9` yield `null`, but only `8` is present. The flag lookups explicitly request `a` and then `b`. In the last map, reading `a` changes the order from `[a, b]` to `[b, a]`.
Related lessons
- Java · Lesson 13 · Advanced · Version: 2
- Java · Lesson 17 · Advanced · Version: 2
- Java · Lesson 18 · Advanced · Version: 2
- Java · Lesson 36 · Advanced · Version: 2
- Java · Lesson 35 · Basic · Version: 2
- Java · Lesson 42 · Basic · Version: 2
- Java · Lesson 58 · Basic · Version: 2
- Java · Lesson 40 · Intermediate · Version: 2
- Java · Lesson 43 · Intermediate · Version: 2
Sources
- Map (Java SE 21 & JDK 21) — get(Object)
- Map (Java SE 21 & JDK 21) — getOrDefault(Object, V)
- HashMap (Java SE 21 & JDK 21) — get(Object)
- Object (Java SE 21 & JDK 21) — hashCode()
- LinkedHashMap (Java SE 21 & JDK 21)
- ConcurrentHashMap (Java SE 21 & JDK 21) — get(Object)
- Java Language Specification, Java SE 21 — §4.8 Raw Types
- Java Language Specification, Java SE 21 — Chapter 5. Conversions and Contexts
- Java Language Specification, Java SE 21 — §17.4.5 Happens-before Order