Stream ofNullable(T) method in Java with examples Last Updated : 25 Apr, 2019 Comments Improve Suggest changes Like Article Like Report 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. Syntax: 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: Program 1: Java // Java program to demonstrate // Stream.ofNullable() method import java.util.stream.Stream; public class GFG { public static void main(String[] args) { // Create a stream with null Stream<String> value = Stream.ofNullable(null); // Print values System.out.println("Values of Stream:"); value.forEach(System.out::println); } } The output printed on console of IDE is shown below. Output: Program 2: Java // Java program to demonstrate // Stream.ofNullable method import java.util.ArrayList; import java.util.stream.Stream; public class GFG { public static void main(String[] args) { // Create ArrayList containing names ArrayList<String> list = new ArrayList<String>(); list.add("Aman"); list.add("Suraj"); list.add("Zufaq"); // create a stream with ArrayList Stream<ArrayList<String> > value = Stream.ofNullable(list); // print values System.out.println("Values of Stream:"); value.forEach(System.out::println); } } The output printed on console is shown below. Output: References: https://docs.oracle.com/javase/10/docs/api/java/util/stream/Stream.html#ofNullable(T) Comment More infoAdvertise with us Next Article Stream ofNullable(T) method in Java with examples A AmanSingh2210 Follow Improve Article Tags : Java Java - util package Java-Functions java-stream Practice Tags : Java Similar Reads Optional ofNullable() method in Java with examples The ofNullable() method of java.util.Optional class in Java is used to get an instance of this Optional class with the specified value of the specified type. If the specified value is null, then this method returns an empty instance of the Optional class. Syntax: public static <T> Optional< 2 min read Optional stream() method in Java with examples 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() Paramete 2 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 Stream mapToDouble() in Java with examples Stream mapToDouble (ToDoubleFunction mapper) returns a DoubleStream consisting of the results of applying the given function to the elements of this stream. Stream mapToDouble (ToDoubleFunction mapper) is an intermediate operation. These operations are always lazy. Intermediate operations are invoke 2 min read Stream peek() Method in Java with Examples In Java, Stream provides an powerful alternative to process data where here we will be discussing one of the very frequently used methods named peek() which being a consumer action basically returns a stream consisting of the elements of this stream, additionally performing the provided action on ea 2 min read Like