Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

util.function.UnaryOperator: practical reference

util.function.UnaryOperator: practical reference: UnaryOperator<T> specializes Function<T,T>: apply accepts a T and returns a result of the same type.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

java.util.function.UnaryOperator

UnaryOperator specializes Function<T,T>: apply accepts a T and returns a result of the same type.

Use it for same-type transformations such as List.replaceAll, state updates, and compose or andThen pipelines.

Matching input and output types imply neither in-place mutation, purity, nor null handling; those belong to the operator implementation.

identity returns its input unchanged, while compose and andThen retain the direction and exception propagation defined by Function.

The example exercises the documented element in a small deterministic program and compares exact standard output.

Examples

public class Main {
    public static void main(String[] args) throws Exception {
        java.util.function.UnaryOperator<Integer> twice = value -> value * 2;
        System.out.println(twice.apply(21));
    }
}

Result

42

The example exercises the documented element in a small deterministic program and compares exact standard output.

Sources