A key is a common identifier that associates a value with a particular record or entity. In stateful processing, it lets an operation look up, update, and store the value belonging to that entity. section 7.1
A stateful count uses the incoming object's identifier as the key, retrieves the current count, increments it, and stores the result. When no value exists, an initializing function supplies the first value. section 7.1
Counting widgets by identifier
The count function calls hashMap.compute(widget.id, ...) to associate each widget ID with its running count, returning 1 for the first occurrence and incrementing later occurrences. section 7.1
Preserving a Kafka record key
A Spring Kafka listener can capture an incoming record's key through the RECEIVED_MESSAGE_KEY header, then produce the processed loan with processedLoan.getCustomerId() as its key. section 12.2.2
Listing 7.2 Stateful function example
public int count(Widget widget) {
int widgetCount = hashMap.compute(widget.id,
(key, value) -> (value == null) ? 1 : value + 1)
return widgetCount;
}
Inspect the compact Java implementation of key-based state: compute a value by identifier, initialize missing state, and increment existing state. section 7.1









