Java Stream filter, map and reduce

Description

Java Stream filter, map and reduce

import java.util.Arrays;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    int sum = numbers.stream()
        .filter(n -> n % 2  == 0)//from   w w  w  .j av  a2 s.  c o  m
        .map(n  -> n * n)
        .reduce(0, Integer::sum);

    System.out.println(sum);
  }
}



PreviousNext

Related