Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

java.util.Map — mapping keys to values

Map<K,V> associates each key with at most one value. Learn lookup, updates, computed values, and map views, with particular attention to null handling and implementation guarantees.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

Map<K, V> map = new HashMap<>();

java.util.Map<K,V> is an interface mapping keys to values; values may repeat. HashMap guarantees no ordering, while TreeMap orders keys. Support for null depends on the implementation. Do not mutate a stored key in a way that affects its comparison.

Map.put(k, v) adds or replaces a value and returns the previous one; Map.remove(k) deletes an entry and returns its old value. Both also return null for an absent entry. Map.get(k) returns the value or null; Map.containsKey(k) distinguishes absence from a mapped null. Map.getOrDefault(k, d) returns d only for absence, not for a mapped null. Map.size() counts entries; Map.containsValue(v) tests whether a value occurs.

Map.compute(k, f) passes the key and current value or null to the function; a null result removes the entry or leaves it absent. Map.computeIfAbsent(k, f) calls the function for an absent entry or a null value; it stores only a non-null result. Map.merge(k, v, f) requires v != null: it stores v for absence or a null value, otherwise combines the old value with v; a null result removes the entry.

Map.keySet() exposes a set of keys, Map.values() a collection of values, and Map.entrySet() a set of Map.Entry<K,V> pairs. These are map-backed views, not copies; removing through a view removes the entry when supported. Map.forEach(action) visits each pair.

Mutating operations may throw UnsupportedOperationException. Computing functions should not modify the same map. Default Map methods guarantee neither atomicity nor synchronization. The example uses TreeMap for deterministic output ordering.

Examples

import java.util.Map;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> counts = new TreeMap<>();
        counts.put("pear", 1);
        counts.merge("pear", 2, Integer::sum);
        counts.computeIfAbsent("apple", key -> 1);
        counts.compute("apple", (key, value) -> value + 1);
        System.out.println(counts.get("pear"));
        System.out.println(counts.getOrDefault("plum", 0));
        System.out.println(counts.containsKey("plum"));
        System.out.println(counts.containsValue(2));
        System.out.println(counts.size());
        System.out.println(counts.keySet());
        System.out.println(counts.values());
        System.out.println(counts.entrySet());
        counts.forEach((key, value) -> System.out.println(key + ":" + value));
        System.out.println(counts.remove("pear"));
        System.out.println(counts.size());
    }
}

Result

3
0
false
true
2
[apple, pear]
[2, 3]
[apple=2, pear=3]
apple:2
pear:3
3
1

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources