The build() method in Java Stream.Builder


The build() method in Stream.Builder class is used to build the stream. It returns the built stream.

The syntax is as follows:

Stream<T>l build()

Import the following package for the Stream.Builder class in Java:

import java.util.stream.Stream;

Declare a Stream.Builder:

Stream.Builder<String> builder = Stream.builder();

Add some elements in the stream:

builder.add("One");
builder.add("Two");
builder.add("Three");

Now, use the build() method:

Stream<String> str = builder.build();

The following is an example displaying how to implement build() method of Stream.Builder in Java:

Example

 Live Demo

import java.util.stream.Stream;
public class Demo {
   public static void main(String[] args) {
      Stream.Builder<String> builder = Stream.builder();
      builder.add("One");
      builder.add("Two");
      builder.add("Three");
      builder.add("Four");
      builder.add("Five");
      builder.add("Six");
      builder.add("Seven");
      builder.add("Eight");
      Stream<String> str = builder.build();
      str.forEach(System.out::println);
   }
}

Output

One
Two
Three
Four
Five
Six
Seven
Eigh

Updated on: 30-Jul-2019

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements