CKTIS / Java
Map.Entry.getKey — reading an entry's key
Map.Entry.getKey() returns the key of one map entry as a value of type K. It takes no arguments; distinguish the entry's key from its value.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
K getKey()
// Usage:
K key = entry.getKey();Map.Entry<K,V> represents a key–value pair. The instance method Map.Entry.getKey has the signature K getKey(): calling entry.getKey() reads the key of type K, not the value of type V returned by entry.getValue().
An entry obtained while iterating over map.entrySet() is connected to the map. Outside iteration, or after modifying the map except through the entry's setValue, its behavior is undefined. After removal, an implementation may, but need not, throw IllegalStateException.
The example uses Map.entry("score", 42) to create an unmodifiable entry disconnected from any map. Map.entry rejects null keys and values. The retrieved key is score.
Examples
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map.Entry<String, Integer> entry = Map.entry("score", 42);
String key = entry.getKey();
System.out.println(key);
}
}Result
score
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.