Normal Consumer can take only one input argument and perform required operation and won’t return any result.
But sometimes our programming requirement to accept 2 input values and perform required operation and not required to return any result. Then we should go for BiConsumer.
BiConsumer is exactly same as Consumer except that it will take 2 input arguments.
@FunctionalInterface public interface BiConsumer<T,U> { void accept(T t, U u); default BiConsumer<T,U> andThen(BiConsumer<? super T,? super U> after) { } }
Program to accept 2 String values and print result of concatenation by using BiConsumer :
package com.codeFactory.biconsumer;import java.util.function.BiConsumer;public class Test { public static void main(String... args) { BiConsumer<String, String> c = (s1, s2) -> System.out.println(s1 + s2); c.accept("Code", "Factory"); } }
Output :
CodeFactory
Demo Program to increment employee Salary by using BiConsumer :