Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Function.identity(): the identity function and collector finisher

`Function.identity()` gives you a function that returns the same reference. As a collector finisher, it lets the accumulation container become the result unchanged.

Version: Java SE 21

Syntax

import java.util.function.Function;

// Static method declaration in java.util.function.Function:
static <T> Function<T, T> identity()

// Typical use:
Function<String, String> same = Function.identity();

Function.identity() is a static generic method on java.util.function.Function, an interface available since Java 8. The call takes no arguments and returns a Function<T, T>, whose input and result have the same type. Its apply(x) behaves like x -> x: it returns the same reference without copying or modifying the object, and returns null for null.

In Collector<T, A, R>, T is the input element type, A the accumulation container type, and R the final result type. finisher() supplies a Function<A, R>. When A and R are the same type and the container should become the result unchanged, pass Function.identity() to the Collector.of overload with an explicit finisher. If the result must be a copy or unmodifiable, use a finisher that performs that transformation.

Passing Function.identity() as the finisher does not automatically set IDENTITY_FINISH. This characteristic lets the reduction skip the finisher call; declare it only if the finisher is an identity function and an unchecked cast from A to R will succeed. The Collector.of overload without a finisher argument sets the characteristic for you.

If an API requires a UnaryOperator<T>, use UnaryOperator.identity(): the behavior is the same, but the declared return type is UnaryOperator<T>, not Function<T, T>.

Examples

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collector;

public class IdentityFinisherDemo {
    public static void main(String[] args) {
        Collector<String, List<String>, List<String>> collector = Collector.of(
                ArrayList::new,
                List::add,
                (left, right) -> {
                    left.addAll(right);
                    return left;
                },
                Function.identity(),
                Collector.Characteristics.IDENTITY_FINISH);

        List<String> input = List.of("Ada", "Lin");
        List<String> buffer = collector.supplier().get();
        input.forEach(name -> collector.accumulator().accept(buffer, name));
        List<String> result = collector.finisher().apply(buffer);

        System.out.println(result == buffer);
        System.out.println(input.stream().collect(collector));
    }
}

Result

true
[Ada, Lin]

The first line, `true`, shows that `==` finds the same reference after a direct finisher call. The second, `[Ada, Lin]`, shows the contents collected from the stream, not whether the stream called the finisher.

Related lessons

  • Java · Lesson 24 · Advanced · Version: 2

Sources