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()
 andÂparallelStream()
 methods of theÂjava.util.Collection
 interface—this means that all the sub-interfaces, includingÂSet
 andÂList
, have these methods too - Two overloadedÂ
stream()
 methods of theÂjava.util.Arrays
 class, which convert arrays and subarrays to streams - TheÂ
of()
,Âgenerate()
, andÂiterate()
 methods of theÂjava.util.stream.Stream
 interface - TheÂ
Stream<Path> list()
,ÂStream<String> lines()
, andÂStream<Path> find()
 methods of theÂjava...