Tuesday, 14 October 2025

How to use BiFunction functional interface in java 8 ?

BiFunction is a Functional interface with only one abstract method apply(T t, U u), which takes two arguments and return a result of type R. It is a generic interface defined as BiFunction<T, U, R>

BiFunction interfaces are typically implemented as lambda expressions.

import java.util.function.BiFunction;
import java.util.function.Function;

public class BiFunctionExample {
public static void main(String[] args) {
BiFunction<String, String, String> biFunction = (s1, s2) -> s1 + s2;
System.out.println(checkStr(biFunction));
Function<String, String> function = r -> "Result : " + r;
System.out.println(biFunction.andThen(function).apply("b", "c"));
}

private static Boolean checkStr(BiFunction<String, String, String> biFunction) {
if ("ab".equals(biFunction.apply("a", "b"))) {
return true;
}
return false;
}
}

Result:

true

Result : bc

No comments:

Post a Comment