Java Stream How to - Pass in Function as parameter








Question

We would like to know how to pass in Function as parameter.

Answer

/*from  w  w  w  . j  av  a  2 s . c o m*/
import java.util.function.Function;

public class Main {

  public static void main(String[] args) {

    String result = calc((a) -> "Result: " + (a * 2), 10);

    System.out.println(result);
  }

  public static String calc(Function<Integer, String> bi, Integer i) {
    return bi.apply(i);
  }
}

The code above generates the following result.