Monday, November 18, 2024
Google search engine
HomeLanguagesJavaIntStream.Builder add() method in Java

IntStream.Builder add() method in Java

IntStream.Builder add(int 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 IntStream.Builder add(int 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 add() method:

Example 1:




// Java code to show the implementation
// of IntStream.Builder add(int t)
  
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Declaring an empty Stream
        IntStream.Builder b = IntStream.builder();
  
        // Inserting elements into the stream
        // using IntStream.Builder add(int t)
        b.add(4);
        b.add(5);
        b.add(6);
        b.add(7);
  
        // 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 IntStream.Builder add(T t)
  
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Declaring an empty Stream
        IntStream.Builder b = IntStream.builder();
  
        // using IntStream.Builder add(T t)
        b.add(4);
        b.add(5);
        b.add(6);
        b.add(7);
  
        // 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 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 {
            b.add(50);
        }
        catch (Exception e) {
            System.out.println("Exception thrown "
                               + "when now adding element into the stream: "
                               + e);
        }
    }
}


Output:

Stream successfully built
4
5
6
7
Exception thrown when now adding element into the stream: java.lang.IllegalStateException
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments