BiFunction example

Description

BiFunction represents a function that accepts two arguments and produces a result. This is the two-arity specialization of Function.

Example

The following example shows how to use BiFunction.


import java.util.function.BiFunction;
//  w w  w  .  ja  v  a2 s. co  m
public class Main {
  public static void main(String[] args) {
    BiFunction<String, String,String> bi = (x, y) -> {      
      return x + y;
    };

    System.out.println(bi.apply("java2s.com", " tutorial"));
  }
}

The code above generates the following result.

Example 2

The following code shows how to use BiFunction as a parameter.


import java.util.function.BiFunction;
public class Main {
/* w  w  w  . j ava 2s . c om*/
  public static void main(String[] args) {
    Calculator calculator = new Calculator();
    String result = calculator.calc((a, b) -> ": " + (a * b),3,  5);

    System.out.println(result);

  }
}

class Calculator {
  public String calc(BiFunction<Integer, Integer, String> bi, Integer i1, Integer i2) {
      return bi.apply(i1, i2);
  }
}

The code above generates the following result.