What is the IntStream.Builder accept() method in Java

Java 8Object Oriented ProgrammingProgramming

Insert an element into IntStream using the IntStream.Builder accept() method. It adds element to the stream being built.

The syntax is as follows:

void accept(int t)

Here, parameter t is the input argument.

The elements are inserted as shown below in the stream:

builder.accept(10);
builder.accept(15);
builder.accept(25);
builder.accept(39);
builder.accept(45);

The following is an example to implement IntStream.Builder accept() method in Java:

Example

 Live Demo

import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream.Builder builder = IntStream.builder();
      System.out.println("Elements of the stream...");
      builder.accept(10);
      builder.accept(15);
      builder.accept(25);
      builder.accept(39);
      builder.accept(45);
      builder.accept(55);
      builder.accept(80);
      builder.build().forEach(System.out::println);
   }
}

output

Elements of the stream...
10
15
25
39
45
55
80
raja
Published on 01-Apr-2019 12:32:51
Advertisements