Creating a stream
There are many ways to create a stream—an object of the Stream
type or any of the numeric interfaces. We have grouped them by classes and interfaces that have methods creating Stream objects. We did so for the reader's convenience, to provide a better overview, so that it will be easier for the reader to find them if needed.
Stream interface
This group of Stream
factories is composed of static methods that belong to the Stream
interface.
empty(), of(T t), ofNullable(T t)
The following three methods create either empty or single-element Stream
objects:
Stream<T> empty()
: Creates an empty sequentialStream
object.Stream<T> of(T t)
: Creates a sequential single-elementStream
object.Stream<T> ofNullable(T t)
: Creates a sequentialStream
object containing a single element if thet
parameter is non-null; otherwise, creates an empty Stream.
The following code demonstrates the usage of the preceding methods:
Stream.empty().forEach(System.out::println); //prints nothing...