Java Reference ยท Collections
The Map interface
Reference for java.util.Map: key-value mappings, the core methods, views over keys, values and entries, and how the main implementations differ.
Map<K, V> associates keys with values. Each key appears at most once and maps to at most one value. It is not a Collection, but it exposes three collection views: keySet(), values() and entrySet().
You pick an implementation for its behavior: HashMap for speed, LinkedHashMap for insertion (or access) order, TreeMap for sorted keys, and EnumMap for enum keys. Code against the interface and keep the concrete type in one place.
Map<String, Integer> ages = new HashMap<>(); ages.put("Ann", 31); int age = ages.get("Ann");
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public interface Map<K, V>
| Method | Description |
|---|---|
V put(K key, V value) |
Stores the mapping; returns the previous value or null. |
V get(Object key) |
Value for the key, or null if absent. |
V remove(Object key) |
Removes the key; returns the old value or null. |
boolean containsKey(Object), containsValue(Object) |
Membership tests. containsValue is O(n). |
Set<K> keySet(), Collection<V> values(), Set<Map.Entry<K,V>> entrySet() |
Live views. |
getOrDefault, putIfAbsent, computeIfAbsent, computeIfPresent, compute, merge, replace, replaceAll, forEach |
Convenience methods, see the individual pages. |
static Map.of(...), Map.ofEntries(...), Map.copyOf(map), Map.entry(k, v) |
Immutable factories (Java 9, copyOf Java 10). |
Usage notes
get()returnsnullboth for a missing key and for a key mapped tonull. UsecontainsKey()orgetOrDefault()when the difference matters.- Keys should be immutable, or at least must not change in ways that affect
equals/hashCode(or the ordering for aTreeMap). - Null support varies:
HashMapandLinkedHashMapallow onenullkey and any number ofnullvalues;TreeMaprejectsnullkeys with natural ordering;Map.ofandConcurrentHashMapreject all nulls. - Order is an implementation property.
HashMapiteration order is unspecified. - Views write through, but you cannot add to
keySet()orvalues(). Map.equals()compares entries, regardless of implementation or order.- Modify-while-iterating with anything but the iterator or view methods throws
ConcurrentModificationException. - Thread safety: none of the standard maps are synchronized except legacy
Hashtable; useConcurrentHashMapfor shared mutable state.
Related
- HashMap, LinkedHashMap, TreeMap, Map.of(), Map.entrySet()
- Lessons: HashMap, Collections framework