Java Stream How to - Pass in BiFunction with Lambda








Question

We would like to know how to pass in BiFunction with Lambda.

Answer

import java.util.function.BiFunction;
/*from   w  w  w .j  ava  2 s.c  o m*/
class Calculator {
  public String calc(BiFunction<Integer, Integer, String> bi, Integer i1, Integer i2) {
    return bi.apply(i1, i2);
  }
}

public class Main {
  public static void main(String[] args) {
    Calculator calculator = new Calculator();
    String result = calculator.calc((a, b) -> "Result: " + (a * b), 3, 5);

    System.out.println(result);
  }
}

The code above generates the following result.