Tutorialspoint

1 Answer
Nancy Den

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

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.