BiConsumer Functional Interface in Java

BiConsumer interface in Java is an extension of the Consumer interface. It represents an operation that accepts two input arguments and returns no result. The interface has the accept() method, just like the original Consumer interface, but it accepts only two parameters.

BiConsumer<T,U>

@FunctionalInterface
public interface BiConsumer<T, U> {
    /**
     * Performs this operation on the given arguments.
     *
     * @param t the first input argument
     * @param u the second input argument
     */
    void accept(T t, U u);

    /**
     * Returns a composed {@code BiConsumer} that performs, in sequence, this
     * operation followed by the {@code after} operation. If performing either
     * operation throws an exception, it is relayed to the caller of the
     * composed operation.  If performing this operation throws an exception,
     * the {@code after} operation will not be performed.
     *
     * @param after the operation to perform after this operation
     * @return a composed {@code BiConsumer} that performs in sequence this
     * operation followed by the {@code after} operation
     * @throws NullPointerException if {@code after} is null
     */
    default BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after) {
        Objects.requireNonNull(after);
        return (l, r) -> {
            accept(l, r);
            after.accept(l, r);
        };
    }
}


T
 represents the type of the first argument, and U represents the type of the second argument.

It also contains one default method andThen() that accepts a BiConsumer as the input and returns a BiConsumer.

Implementing the BiConsumer interface

Example 1:

class Test {

  public static void main(String[] args) {
    BiConsumer<String, String> concatStrings = (str1, str2) -> {
      System.out.println("The new string is: " + str1.concat(str2));
    };
    concatStrings.accept("Hello ", "Java!");
  }
}
Output: The new string is: Hello Java!


Here, inside the braces <>, we specified what types are the inputs, and we used a Lambda expression to write the implementation of the BiConsumer interface. Between the parentheses (), we have the input parameters, and inside the curly braces {}, we wrote a logic  that concatenated the input parameters.

Example 2:

In this example, you will see how to use the andThen() method.

Let’s write a program that will take two String parameters, return the result of the concatenation, and then calculate which String has more characters and return that information to us.

class Test {

  public static void main(String[] args) {

    BiConsumer<String, String> concatStrings = (str1, str2) -> {
      System.out.println("The new string is: " + str1.concat(str2));
    };

    BiConsumer<String, String> compareStringSizes = (str1, str2) -> {
      if (str1.length() > str2.length()) {
        System.out.println("The first string has more characters.");
      } else {
        System.out.println("The second string has more characters.");
      }
    };

    concatStrings.andThen(compareStringSizes).accept("Hello ", "Java");
  }
}

The output after running the above program is:

Output:
The new string is: Hello Java
The first string has more characters.

Here, we first chained the concatStrings BiConsumer with the compareStringSizes, and then we used the accept() method to pass the input parameters.

That was all about the Biconsumer interface in Java. I hope this tutorial was helpful to you. To learn more, check out other Java Functional Programming tutorials.

 

Leave a Reply

Your email address will not be published. Required fields are marked *