The accept() method in Java Stream.Builder


Add an element to the stream using the accept() method of Java Stream.Builder.

The following is the syntax:

void accept(T t)

Here, t is the argument to be inserted.

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

import java.util.stream.Stream;

First, declare a Stream.Builder:

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

Now, use the accept() method:

builder.accept("Demo");
builder.accept("Text");

The following is an example displaying how to implement accept() 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.accept("Demo");
      builder.accept("Text");
      Stream<String> str = builder.build();
      str.forEach(System.out::println);
   }
}

output

Demo
Text

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements