OptionalInt stream() method in Java with examples
The stream() method help us to get value contain by OptionalInt as IntStream. If a value is present, method returns a sequential IntStream containing only that value, otherwise returns an empty IntStream.
Syntax:
public IntStream stream()
Parameters: This method accepts nothing.
Return value: This method returns the optional value as an IntStream.
Below programs illustrate stream() method:
Program 1:
import java.util.OptionalInt;
import java.util.stream.IntStream;
public class GFG {
public static void main(String[] args)
{
OptionalInt opInt = OptionalInt.of( 452146 );
IntStream out = opInt.stream();
System.out.println( "Value:" );
out.forEach(System.out::println);
}
}
|
Output:
Program 2:
import java.util.OptionalInt;
import java.util.stream.IntStream;
public class GFG {
public static void main(String[] args)
{
OptionalInt opInt = OptionalInt.empty();
IntStream out = opInt.stream();
if (out.count() == 0 )
System.out.println( "opInt is empty" );
}
}
|
Output:
References: https://docs.oracle.com/javase/10/docs/api/java/util/OptionalInt.html#empty()
Last Updated :
28 May, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...