LongStream.Builder accept() method in Java

LongStream.Builder accept(long t) is used to insert an element into the element in the building phase of stream. It accepts an element to the stream being built.

Syntax:

void accept(long t)

Parameters: This method accepts a mandatory parameter t which is the element to input into the stream.

Exceptions: This method throws IllegalStateException when the builder has already transitioned to the built state. It means that the stream has entered the built phase and now no it can’t be changed. Hence no more elements can be accepted into the stream.

Below are the examples to illustrate accept() method:



Example 1:

// Java code to show the implementation
// of LongStream.Builder accept(long t)
  
import java.util.stream.LongStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Declaring an empty Stream
        LongStream.Builder b = LongStream.builder();
  
        // Inserting elements into the stream
        // using LongStream.Builder accept(long t)
        b.accept(4L);
        b.accept(5L);
        b.accept(6L);
        b.accept(7L);
  
        // Creating the Stream
        // The stream has now entered the built phase
        // printing the elements
        System.out.println("Stream successfully built");
        b.build().forEach(System.out::println);
    }
}

Output:

Stream successfully built
4
5
6
7

Example 2: To illustrate IllegalStateException

// Java code to show the implementation
// of LongStream.Builder accept(T t)
  
import java.util.stream.LongStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Declaring an empty Stream
        LongStream.Builder b = LongStream.builder();
  
        // using LongStream.Builder accept(T t)
        b.accept(4L);
        b.accept(5L);
        b.accept(6L);
        b.accept(7L);
  
        // Creating the Stream
        // The stream has now entered the built phase
        // printing the elements
        System.out.println("Stream successfully built");
        b.build().forEach(System.out::println);
  
        // Trying to accept another element into the stream
        // Since the Stream is in built phase
        // This operation is not possible now
        // Hence accept() will throw exception now
  
        try {
            b.accept(50L);
        }
        catch (Exception e) {
            System.out.println("Exception thrown "
                               + "when now accepting element into the stream: "
                               + e);
        }
    }
}

Output:

Stream successfully built
4
5
6
7
Exception thrown when now accepting element into the stream: java.lang.IllegalStateException


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.