Java Stream How to - Create recursive Lambda








Question

We would like to know how to create recursive Lambda.

Answer

/*from www . j a  va2  s .  c om*/
import java.util.function.UnaryOperator;

public class Main {
  static UnaryOperator<Integer> factorial = null;

  public static void main(String[] args) {

    factorial = i -> i == 0 ? 1 : i * factorial.apply(i - 1);

    System.out.println(factorial.apply(6));

  }
}

The code above generates the following result.