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 // Java code to show the implementation // of Stream.Builder accept(T t) import java.util.stream.Stream; class GFG { // Driver code public static void main(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 // Java code to show the implementation // of Stream.Builder accept(T t) import java.util.stream.Stream; class GFG { // Driver code public static void main(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 Comment More infoAdvertise with us Next Article Stream.Builder accept() method in Java S Sahil_Bansall Follow Improve Article Tags : Misc Java Java - util package Java-Functions java-stream Java-Stream-Builder +2 More Practice Tags : JavaMisc Similar Reads Stream.Builder add() method in Java Stream.Builder add(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: default Stream.Builder<T> add(T t) Parameters: This method adds a mandatory parameter t which is the element to input into the stream. Ex 2 min read 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. Excepti 2 min read IntStream.Builder accept() method in Java IntStream.Builder accept(int 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(int t) Parameters: This method accepts a mandatory parameter t which is the element to input into the stream. Exceptions 2 min read Arrays.stream() Method in Java Arrays.stream() method is used to get a sequential Stream from the elements of an array passed as a parameter. Below is a simple example that uses Arrays.stream() to convert a String array into a Stream for sequential processing.Example:Java// Java program to demonstrate Arrays.stream() method impor 3 min read DoubleStream.Builder accept() method in Java DoubleStream.Builder accept(double 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(double t) Parameters: This method accepts a mandatory parameter t which is the element to input into the stream. E 2 min read Stream.Builder build() in Java Stream.Builder build() builds the stream, transitioning this builder to the built state. Syntax : Stream<T> build() Exceptions : IllegalStateException : If the builder has already transitioned to the built state, IllegalStateException is thrown. It signals that a method has been invoked at an 2 min read LongStream.Builder add(long t) in Java LongStream.Builder add(long 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: default LongStream.Builder add(long t) Parameters: This method accepts a mandatory parameter t which is the element to input into the st 2 min read Stream of() method in Java Stream of(T t) Stream of(T t) returns a sequential Stream containing a single element. Syntax : static Stream of(T t) Parameters: This method accepts a mandatory parameter t which is the single element in the Stream. Return Value: Stream of(T t) returns a sequential Stream containing the single spec 2 min read Stream peek() Method in Java with Examples In Java, Stream provides an powerful alternative to process data where here we will be discussing one of the very frequently used methods named peek() which being a consumer action basically returns a stream consisting of the elements of this stream, additionally performing the provided action on ea 2 min read Stream generate() method in Java with examples Stream generate(Supplier<T> s) returns an infinite sequential unordered stream where each element is generated by the provided Supplier. This is suitable for generating constant streams, streams of random elements, etc. Syntax : static <T> Stream<T> generate(Supplier<T> s) Wh 1 min read Like