DoubleStream.Builder add(double t) in Java with Examples

DoubleStream.Builder add(double 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 DoubleStream.Builder add(double 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show the implementation
// of DoubleStream.Builder add(double t)
  
import java.util.stream.DoubleStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Declaring an empty Stream
        DoubleStream.Builder b = DoubleStream.builder();
  
        // Inserting elements into the stream
        // using DoubleStream.Builder add(double t)
        b.add(4.4);
        b.add(5.84);
        b.add(6);
        b.add(7.47);
  
        // 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);
    }
}

chevron_right


Output:

Stream successfully built
4.4
5.84
6.0
7.47

Example 2: To illustrate IllegalStateException

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Stream successfully built
4.7
5.9
6.0
7.785
Exception thrown when now adding element into the stream: java.lang.IllegalStateException


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

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.