Skip to content

Latest commit

 

History

History
47 lines (38 loc) · 1.38 KB

File metadata and controls

47 lines (38 loc) · 1.38 KB

BiConsumer

BiConsumer is a functional interface that represents a Consumer accepting two parameters.

Normal Consumer can take only one input argument and perform required operation and won't return any result. BiConsumer can take two inputs perform required operation and won't return any result. It is exactly same as Consumer except it wll take 2 input arguments.

It is a functional interface present in java.util.function pacckage and has a single abstract method accept().

void accept(T t, U u);

Here, T and U represent the types of the input parameters. The accept method implementation defines the action to be performed on the input arguments.

@FunctionalInterface
public interface BiConsumer<T, U> {
    //SAM (Single Abstract Method)
    void accept(T t, U u);

    //BiConsumer chaining
    //andThen
    default BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after) {
        ...
    }
}

Example: Program to accept 2 String values and print their concatenation.

import java.util.function.BiConsumer;

class App {
    public static void main(String[] args) {
        BiConsumer<String, String> biCon = (s1, s2) -> System.out.println(s1 + s2);
        biCon.accept("Hello", "World"); 
    }
}

Output:

HelloWorld

Example: BiConsumer