Optional stream() method in Java with examples Last Updated : 30 Jul, 2019 Comments Improve Suggest changes Like Article Like Report The stream() method of java.util.Optional class in Java is used to get the sequential stream of the only value present in this Optional instance. If there is no value present in this Optional instance, then this method returns returns an empty Stream. Syntax: public Stream<T> stream() Parameters: This method do not accept any parameter. Return value: This method returns the sequential stream of the only value present in this Optional instance. If there is no value present in this Optional instance, then this method returns an empty Stream. Below programs illustrate stream() method: Note: Below programs require JDK 9 and above to execute. Program 1: Java // Java program to demonstrate // Optional.stream() method import java.util.*; public class GFG { public static void main(String[] args) { // create a Optional Optional<Integer> op = Optional.of(9455); // print value System.out.println("Optional: " + op); // get the Stream System.out.println("Getting the Stream:"); op.stream().forEach(System.out::println); } } Output: Optional: Optional[9455] Getting the Stream: 9455 Program 2: Java // Java program to demonstrate // Optional.stream() method import java.util.*; public class GFG { public static void main(String[] args) { // create a Optional Optional<Integer> op = Optional.empty(); // print value System.out.println("Optional: " + op); try { // get the Stream System.out.println("Getting the Stream:"); op.stream().forEach(System.out::println); } catch (Exception e) { System.out.println(e); } } } Output: Optional: Optional.empty Getting the Stream: Reference: https://docs.oracle.com/javase/9/docs/api/java/util/Optional.html#stream-- Comment More infoAdvertise with us Next Article Optional stream() method in Java with examples S ShubhamMaurya3 Follow Improve Article Tags : Java Java - util package Java-Functions Java-Optional Practice Tags : Java Similar Reads 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 m 1 min read OptionalLong stream() method in Java with examples 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 valu 1 min read OptionalDouble stream() method in Java with examples The stream() method help us to get double value contain by OptionalDouble as DoubleStream.If a value is present, method returns a sequential DoubleStream containing only that value, otherwise returns an empty DoubleStream. Syntax: public DoubleStream stream() Parameters: This method accepts nothing. 1 min read Optional or() method in Java with examples The or() method of java.util.Optional class in Java is used to get this Optional instance if any value is present. If there is no value present in this Optional instance, then this method returns an Optional instance with the value generated from the specified supplier. Syntax: public Optional<T 2 min read Stream min() method in Java with Examples Stream.min() returns the minimum element of the stream based on the provided Comparator. A Comparator is a comparison function, which imposes a total ordering on some collection of objects. min() is a terminal operation which combines stream elements and returns a summary result. So, min() is a spec 3 min read Like