Java Stream How to - Create int stream from 0 to 10 and get average








Question

We would like to know how to create int stream from 0 to 10 and get average.

Answer

//w  w  w  . j  a  va 2 s.  c om
import java.util.stream.IntStream;

public class Main {

  public static void main(String[] args) throws Exception {

    IntStream
        .range(0, 10)
        .average()
        .ifPresent(System.out::println);
  }
}

The code above generates the following result.