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.
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 accepted into the stream.
Below are the examples to illustrate accept() method:
Example 1:
import java.util.stream.DoubleStream;
class GFG {
public static void main(String[] args)
{
DoubleStream.Builder b = DoubleStream.builder();
b.accept( 45.54 );
b.accept( 415.2541 );
b.accept( 12.21 );
b.accept( 778 );
System.out.println( "Stream successfully built" );
b.build().forEach(System.out::println);
}
}
|
Output:
Stream successfully built
45.54
415.2541
12.21
778.0
Example 2: To illustrate IllegalStateException
import java.util.stream.DoubleStream;
class GFG {
public static void main(String[] args)
{
DoubleStream.Builder b = DoubleStream.builder();
b.accept( 45.54 );
b.accept( 415.2541 );
b.accept( 12.21 );
b.accept( 778 );
System.out.println( "Stream successfully built" );
b.build().forEach(System.out::println);
try {
b.accept( 75.1452 );
}
catch (Exception e) {
System.out.println( "Exception thrown "
+ "when now accepting element into the stream: "
+ e);
}
}
}
|
Output:
Stream successfully built
45.54
415.2541
12.21
778.0
Exception thrown when now accepting element into the stream: java.lang.IllegalStateException
Last Updated :
06 Dec, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...