The accept() method in Java Stream.Builder

Java 8Object Oriented ProgrammingProgramming

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
raja
Updated on 30-Jul-2019 22:30:25

Advertisements