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