Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

util.function.BiConsumer: practical reference

util.function.BiConsumer: practical reference: BiConsumer<T,U> is a functional operation that accepts two arguments, returns no result, and is normally intended for side effects.

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

Syntax

java.util.function.BiConsumer

BiConsumer<T,U> is a functional operation that accepts two arguments, returns no result, and is normally intended for side effects.

Use accept for paired data such as a key and value, and use andThen to compose two effects in explicit sequence.

andThen runs the current operation first; if it throws, the second operation is skipped and the exception is relayed to the caller.

A null after argument throws NullPointerException when composing; data-null handling and thread safety of side effects belong to the supplied lambda.

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.BiConsumer<String, Integer> print = (text, count) -> System.out.println(text.repeat(count));
        print.accept("A", 3);
    }
}

Result

AAA

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

Sources