Java Stream How to - Reduce to sum long list








Question

We would like to know how to reduce to sum long list.

Answer

import java.util.stream.Stream;

public class Main {
  public static void main(String...args){
    long l = Stream.iterate(1L, i -> i + 1).limit(3).reduce(Long::sum).get();
    System.out.println(l);
  }
}

The code above generates the following result.