IntStream static method builder() in Java
A IntStream static method builder used to create a builder for an IntStream. It returns a builder for an IntStream.
Syntax
 1 static IntStream.Builder builder()
return : It returns a builder for an IntStream.
static method builder
 1 package com.java;
 2 
 3 import java.util.stream.IntStream;
 4 
 5 public class Main {
 6 
 7     public static void main(String[] args) {
 8 
 9         // create stream builder
 10         IntStream.Builder builder = IntStream.builder();
 11         // adding elements to builder
 12         builder.add(12);
 13 		builder.add(34);
 14 		builder.add(23);
 15 		builder.add(54);
 16 		builder.add(67);
 17 
 18         // build stream from builder
 19         IntStream stream = builder.build();
 20         System.out.println("The resulting stream elements :");
 21         stream.forEach(System.out::println);
 22     }
 23 }
In the above example, a Stream builder object is creating by calling a static method builder(). An elements is added to stream builder by calling a add() method which returns a resulting builder object after inserting new element. A builder object build() method called that converts a builder to IntStream that assigned to the variable and print.
Output
 1 The resulting stream elements :
 2 12
 3 34
 4 23
 5 54
 6 67

chain add() method to insert multiple elements

chain add() method to insert multiple elements
 1 package com.java;
 2 
 3 import java.util.stream.IntStream;
 4 
 5 public class Main {
 6 
 7     public static void main(String[] args) {
 8 
 9         // create stream builder
 10         IntStream.Builder builder = IntStream.builder();
 11         // adding elements to builder by chaining add() method
 12         builder.add(12).add(34).add(23).add(54);
 13 
 14         // build stream from builder
 15         IntStream stream = builder.build();
 16         System.out.println("The resulting stream elements :");
 17         stream.forEach(System.out::println);
 18     }
 19 }
In the above example, an elements is added to stream builder by calling a add() that returns a builder object after adding new element. Hence, it allows to chain add() method to insert multiple elements. A builder object build() method called that converts a builder to IntStream that assigned to the variable and print.
Output
 1 The resulting stream elements :
 2 12
 3 34
 4 23
 5 54

builder() with accept

builder() with accept
 1 package com.java;
 2 
 3 import java.util.stream.IntStream;
 4 
 5 public class Main {
 6 
 7     public static void main(String[] args) {
 8 
 9         // create stream builder
 10         IntStream.Builder builder = IntStream.builder();
 11         // adding elements to builder
 12         builder.accept(12);
 13         builder.accept(34);
 14         builder.accept(23);
 15         builder.accept(54);
 16 
 17         // build stream from builder
 18         IntStream stream = builder.build();
 19         System.out.println("The resulting stream elements :");
 20         stream.forEach(System.out::println);
 21     }
 22 }
In the above example, a static method builder() method called that creates a builder. A builder method accept() called by passing a value that add new element in the stream builder, similar to add() method but it does not return value. A add() method internally calls the builder accept() method.
Output
 1 The resulting stream elements :
 2 12
 3 34
 4 23
 5 54
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us