Stream initialization
There are many ways to create and initialize a stream – an object of type Stream
or any of the numeric interfaces. We grouped them by classes and interfaces that have stream-creating methods. We did it for your convenience so that it would be easier for you to remember and find them if need be.
Stream interface
This group of Stream
factories is composed of static methods that belong to the Stream
interface.
empty()
The Stream<T> empty()
method creates an empty stream that does not emit any element:
Stream.empty().forEach(System.out::println); //prints nothing
The forEach()
Stream
method acts similarly to the forEach()
Collection
method and applies the passed-in function to each of the stream elements:
new ArrayList().forEach(System.out::println); //prints nothing
The result is the same as creating a stream from an empty collection:
new ArrayList().stream().forEach(System.out::println); ...