The ofNullable(T) method returns a sequential Stream containing a single element if this stream is non-null otherwise method returns an empty Stream. It helps to handle the null stream and NullPointerException.
static <T> Stream<T> ofNullable(T t)
Parameters: This method accepts a single parameter t which is the single element of which the Stream is to be returned.
Return value: This method returns a stream with a single element if the specified element is non-null, otherwise an empty stream.
Below programs illustrate ofNullable(T) method:
The output printed on console of IDE is shown below.
The output printed on console is shown below.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.