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.
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 add() method:
Example 1:
// Java code to show the implementation // of Stream.Builder add(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 add(T t) str_b.add("Geeks"); str_b.add("for"); str_b.add("GeeksforGeeks"); str_b.add("Data Structures"); str_b.add("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); } } |
Stream successfully built Geeks for GeeksforGeeks Data Structures Geeks Classes
Example 2: To illustrate IllegalStateException
// Java code to show the implementation // of Stream.Builder add(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 add(T t) str_b.add("5"); str_b.add("6"); str_b.add("7"); str_b.add("8"); str_b.add("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 add() will throw exception now try { str_b.add("50"); } catch (Exception e) { System.out.println("Exception thrown " + "when now adding element into the stream: " + e); } } } |
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 and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.util.ArrayList.add() Method in Java
- DelayQueue add() method in Java with Examples
- ArrayBlockingQueue add() method in Java
- LinkedList add() Method in Java
- HashSet add() Method in Java
- TreeSet add() Method in Java
- PriorityQueue add() Method in Java
- ArrayDeque add() Method in Java
- LinkedBlockingDeque add() method in Java
- GregorianCalendar add() Method in Java
- Vector add() Method in Java
- IntStream.Builder add() method in Java
- PriorityBlockingQueue add() Method in Java
- StringJoiner add() method in Java
- LinkedTransferQueue add() method in Java
- ConcurrentSkipListSet add() method in Java
- ConcurrentLinkedDeque add() method in Java
- Deque add() method in Java
- ConcurrentLinkedQueue add() method in Java
- Queue add() method in Java
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.

