The reason these operators return an Optional is that, generally, there may be not a single element emitted (the Stream object is empty). An Optional object allows checking for the presence of the value before committing to getting it, thus avoiding unexpected behavior.
Articles related to : streams
Java streams 21. Class Optional
The purpose of an object of Optional class is to avoid returning null as it may cause NullPointerException. Instead, an Optional object provides methods that allow checking the presence of a value and substituting it with a predefined value if the return value is null.
Java streams 20. AnyMatch, allMatch, or noneMatch
Terminal operations either return values of other types or do not return anything at all (produce just side effects). They do not allow other operations to be applied and close the stream.
Java streams 19. Count, forEach, or forEachOrdered
A terminal operation produces side effects or/and a single value. The operation that produces side effects only can be used for an infinite Stream. But a terminal operation that produces a single value never returns any value for an infinite Stream.
Java streams 18. FlatMap
This is an intermediate operation that transforms each element of the stream and produces a stream of values of the same or different type by applying the specified function.
Java streams 17. Map
This is an intermediate operation that transforms each element of the stream and produces a value of the same or different type by applying the specified function.
Java streams 16. Filter, skip, or sort
An intermediate operation returns a Stream object that emits the same or modified value(s) of the same or different type than the stream source. In this installment, we will discuss operations that do not change either element value or type. They just do not allow some elements to propagate downstream.
Java streams 15. Stream factory methods.
The Stream interface factory methods are the methods that create a Stream object. Their implementation includes call to a class constructor and that is the advantage of factory methods: they can do much more than just a constructor.
Java streams 14. Stream factory methods and operations
Some of the Stream interface methods create a Stream object (we call them factory methods), while others process the elements emitted by the created Stream object (we call them operations).
Java streams 13. Create using BitSet.stream(), JarFile.stream(), or Pattern.splitAsStream()
Create a stream using IntStream BitSet.stream(), Stream JarFile.stream(), or Stream Pattern.splitAsStream(java.lang.CharSequence).