The add() method in Java Stream.Builder


Use the add() method to insert elements in the Stream.Builder. The element to be added is a parameter for the add().

The following is the syntax:

default Stream.Builder<T> add(T t)

Here, t is the element to be added.

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

import java.util.stream.Stream;

At first, create Stream.Builder:

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

Now, add elements using add() method:

builder.add("This");
builder.add("is");
builder.add("it!");

Here is an example displaying how to implement add() 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("This");
      builder.add("is");
      builder.add("it!");
      Stream<String> str = builder.build();
      str.forEach(System.out::println);
   }
}

output

This
is
it!

Updated on: 30-Jul-2019

312 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements