Open In App

Stream.Builder accept() method in Java

Last Updated : 06 Dec, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report

Stream.Builder accept(T t) is used to insert an element into the element in the building phase of stream. It adds an element to the stream being built.

Syntax:

void accept(T 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 added into the stream.

Below are the examples to illustrate accept() method:

Example 1:




// Java code to show the implementation
// of Stream.Builder accept(T t)
  
importjava.util.stream.Stream;
  
classGFG {
  
    // Driver code
    publicstaticvoidmain(String[] args)
    {
  
        // Declaring an empty Stream
        Stream.Builder<String> str_b = Stream.builder();
  
        // Inserting elements into the stream
        // using Stream.Builder accept(T t)
        str_b.accept("Geeks");
        str_b.accept("for");
        str_b.accept("GeeksforGeeks");
        str_b.accept("Data Structures");
        str_b.accept("Geeks Classes");
  
        // Creating the String Stream
        // The stream has now entered the built phase
        Stream<String> s = str_b.build();
  
        // printing the elements
        System.out.println("Stream successfully built");
        s.forEach(System.out::println);
    }
}

Output:

 Stream successfully built Geeks for GeeksforGeeks Data Structures Geeks Classes 

Example 2: To illustrate IllegalStateException




// Java code to show the implementation
// of Stream.Builder accept(T t)
  
importjava.util.stream.Stream;
  
classGFG {
  
    // Driver code
    publicstaticvoidmain(String[] args)
    {
  
        // Declaring an empty Stream
        Stream.Builder<String> str_b = Stream.builder();
  
        // using Stream.Builder accept(T t)
        str_b.accept("5");
        str_b.accept("6");
        str_b.accept("7");
        str_b.accept("8");
        str_b.accept("9");
  
        // Creating the String Stream
        // The stream has now entered the built phase
        Stream<String> s = str_b.build();
  
        // printing the elements
        System.out.println("Stream successfully built");
        s.forEach(System.out::println);
  
        // Trying to add 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{
            str_b.accept("50");
        }
        catch(Exception e) {
            System.out.println("Exception thrown "
                               + "when now adding element into the stream: "
                               + e);
        }
    }
}

Output:

 Stream successfully built 5 6 7 8 9 Exception thrown when now adding element into the stream: java.lang.IllegalStateException 


Next Article

Similar Reads

close