Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Map.Entry.getValue — read an entry's value

Map.Entry.getValue() reads the value of one key–value pair. It takes no arguments and returns the type V specified by Map.Entry<K,V>.

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

Syntax

V getValue()
// Usage: V value = entry.getValue();

Map.Entry.getValue is an instance method with the signature V getValue(). For Map.Entry<String, Integer>, it returns Integer. Unlike Map.get(key), it reads an entry you already hold instead of looking up a value by key.

The result can be null if the entry stores that value. Reading does not replace the value; the optional setValue method serves that purpose.

For an entry obtained while iterating over Map.entrySet(), do not assume validity outside iteration or after map changes other than through that entry's setValue. After removal, the result is undefined; an implementation may, but need not, throw IllegalStateException.

The example creates an entry independent of any map using Map.entry("points", 42) and prints 42. Documentation scope: Java SE 21; the production runtime is not attested.

Examples

import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map.Entry<String, Integer> entry = Map.entry("points", 42);
        Integer value = entry.getValue();
        System.out.println(value);
    }
}

Result

42

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