Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

util.function.BinaryOperator: practical reference

util.function.BinaryOperator: practical reference: BinaryOperator<T> specializes BiFunction<T,T,T> so both operands and the result have the same type.

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

Syntax

java.util.function.BinaryOperator

BinaryOperator specializes BiFunction<T,T,T> so both operands and the result have the same type.

Use it for a reduction closed over T, and use minBy or maxBy when selection follows a supplied Comparator.

The type does not guarantee associativity; a non-associative operator makes parallel reduction results depend on grouping.

minBy and maxBy reject a null Comparator; when comparison is zero both operands are equivalent by the ordering, so do not rely on identity selection.

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.BinaryOperator<Integer> max = java.util.function.BinaryOperator.maxBy(Integer::compareTo);
        System.out.println(max.apply(3, 7));
    }
}

Result

7

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

Sources