Java IntStream reduce to sum the square

Description

Java IntStream reduce to sum the square

import java.util.stream.IntStream;

public class Main {
  public static void main(String[] args) {
    int[] values = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };

    IntStream intStream = IntStream.of(values);
    // sum of values with reduce method
    int r = intStream.reduce(0, (x, y) -> x + y * y);

    System.out.println("Sum of squares via reduce method:" + r);

  }/*from w  w  w.j a v a 2  s . com*/
}



PreviousNext

Related