Java Stream How to - Create IntStream with builder








Question

We would like to know how to create IntStream with builder.

Answer

//from w w w.j  ava2s  . c  om
import java.util.stream.IntStream;

public class Main {

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


    IntStream
        .builder()
        .add(1)
        .add(3)
        .add(5)
        .add(7)
        .add(11)
        .build()
        .average()
        .ifPresent(System.out::println);
  }
}

The code above generates the following result.