Creating and operating on streams
In this recipe, we will describe how streams can be created and how the operations can be applied to the elements emitted by the streams. The discussion and examples are applicable for a stream of any type, including the specialized numeric streams: IntStream, LongStream, and DoubleStream. The behavior specific to the numeric streams is not presented because it is described in the next recipe, Using numeric streams for arithmetic operations.
Getting ready
There are many ways to create a stream:
- The
stream()andparallelStream()methods of thejava.util.Collectioninterface—this means that all the sub-interfaces, includingSetandList, have these methods too - Two overloaded
stream()methods of thejava.util.Arraysclass, which convert arrays and subarrays to streams - The
of(),generate(), anditerate()methods of thejava.util.stream.Streaminterface - The
Stream<Path> list(),Stream<String> lines(), andStream<Path> find()methods of thejava...