Java IntStream reduce to calculate factorial

Description

Java IntStream reduce to calculate factorial


    /*from   w w w .ja  v a 2  s  .  co  m*/
import java.util.stream.IntStream;

public class Main {
  public static void main(String[] argv) {
    int f = factorial(5);

    System.out.println(f);
  }

  public static int factorial(final int n) {
    return IntStream.rangeClosed(1, n).reduce((x, y) -> x * y).getAsInt();
  }
}



PreviousNext

Related