Java BiFunction Interface with examples

 332 total views,  2 views today

Welcome to Java 8 series.In this tutorial you will learn about Java BiFunction interface with examples.

BiFunction is a Functional interface introduced as part of Java 8. It is available in java.util.function package.

What is the use of BiFunction Interface?

Consider a case where we need to write a lambda expression which accepts two inputs and perform an operation on two inputs and return a result.

Example

public class Test
{
    public static void main(String[] args)
    {
        Operation ops = (String i, String j) -> {
            return i+j;
        };
        String obj = ops.print("aa", "bb");
        System.out.println(obj);
    }
}
interface Operation
{
    String print(String i, String j);
}

Output

aabb

When you look at the above example, we have a function interface with an abstract method which accepts two arguments and returns a String. So a lambda expression needs a target type which needs to be a functional interface and in this example, it is Operation interface.

Instead of creating our own function interface for this scenario, Java has provided us BiFunction interface.

Let us now see BiFunction Interface with examples.

BiFunction Interface

Interface BiFunction<T,U,R>

T – the type of the first argument to the function
U – the type of the second argument to the function
R – the type of the result of the function

BiFunction interface contains an operation that accepts two input arguments and returns a result.

It is a functional interface and the functional method is apply(Object, Object).

Method description

R apply(T t,U u)

t – the first function argument
u – the second function argument,R – the function result

Example of BiFunction interface using lambda.

public static void main(String[] args) {
  BiFunction<String, String,String> biFunction = (String i, String j) -> {
           return i + j;
  };

  String result = biFunction.apply("apple", "orange");
  System.out.println("Result is :"+result);
}

Output

Result is :appleorange

Example of BiFunction interface using Method reference

public static void main(String[] args) {
    BiFunction<String, String,String> biFunction = Test::add;
    String result = biFunction.apply("apple", "orange");
    System.out.println("Result is :"+result);
}
    
static String add(String i,String j) {
    return i+j;
}

Output

Result is :appleorange

If you want to write a lambda expression with two arguments and returns a result, please use Java BiFunction interface.

BiFunction andThen()

default <V> BiFunction<T,U,V> andThen(Function<? super R,? extends V> after)

It is a default method which accepts a Function instance. This will be executed after the current BiFunction is executed.

public static void main(String[] args) {
  BiFunction<String, String,String> biFunction = (String i, String j) -> {
       System.out.println(i+j);
       return i+j;
 };     
  Function<String,String> func = (i)-> {
       System.out.println(i+i);
       return i+i;  
  };        
  biFunction.andThen(func).apply("apple", "orange");
}

References

BiFunction interface official documentation

Thanks for reading. Please a comment and give us a thumbs up.

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.