Stream.Builder accept() method in Java
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.
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:
Stream successfully built Geeks for GeeksforGeeks Data Structures Geeks Classes
Example 2: To illustrate IllegalStateException
Stream successfully built 5 6 7 8 9 Exception thrown when now adding element into the stream: java.lang.IllegalStateException
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.