Skip to content
Tutorials Center
My learning

Topics

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.

Java
Map<String, Integer> ages = new HashMap<>();
ages.put("Ann", 31);
int age = ages.get("Ann");

Try it

Java The core operations

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.

Java Views are live

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

Java
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() returns null both for a missing key and for a key mapped to null. Use containsKey() or getOrDefault() when the difference matters.
  • Keys should be immutable, or at least must not change in ways that affect equals/hashCode (or the ordering for a TreeMap).
  • Null support varies: HashMap and LinkedHashMap allow one null key and any number of null values; TreeMap rejects null keys with natural ordering; Map.of and ConcurrentHashMap reject all nulls.
  • Order is an implementation property. HashMap iteration order is unspecified.
  • Views write through, but you cannot add to keySet() or values().
  • 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; use ConcurrentHashMap for shared mutable state.