Summary of Stream Building Methods – Streams
Summary of Stream Building Methods
Selected methods for building streams from various data sources are listed in Table 16.1. The first column lists the method names and the reference type that provides them. For brevity, the parameters of the methods are omitted. Note that some methods are overloaded. The prefix NumType stands for Int, Long, or Double. A reference is also provided where details about the method can be found. The remaining four columns indicate various aspects of a stream: the type of stream returned by a method, whether the stream is finite or infinite, whether it is sequential or parallel, and whether it is ordered or unordered (p. 891).
Table 16.1 Summary of Stream Building Methods
Method | Returned stream type | Finite/Infinite | Sequential/Parallel | Ordered/Unordered |
Stream.empty() NumTypeStream.empty() (p. 893) | Stream<T> NumTypeStream | Finite | Sequential | Ordered |
Stream.of() Stream.ofNullable() NumTypeStream.of() (p. 893) | Stream<T> Stream<T> NumTypeStream | Finite | Sequential | Ordered |
Stream.generate() NumTypeStream.generate() (p. 895) | Stream<T> NumTypeStream | Infinite | Sequential | Unordered |
Stream.iterate() NumTypeStream.iterate (p. 895) | Stream<T> NumTypeStream | Infinite | Sequential | Ordered |
Stream.concat() NumTypeStream.concat() (p. 895) | Stream<T> NumTypeStream | Finite if both finite | Parallel if either parallel | Ordered if both ordered |
Collection.stream() (p. 897) | Stream<T> | Finite | Sequential | Ordered if collection ordered |
Collection.parallelStream() (p. 897) | Stream<T> | Finite | Parallel | Ordered if collection ordered |
Arrays.stream() (p. 898) | Stream<T> NumTypeStream | Finite | Sequential | Ordered |
IntStream.range() IntStream.rangeClosed() LongStream.range() LongStream.rangeClosed() (p. 898) | IntStream IntStream LongStream LongStream | Finite | Sequential | Ordered |
Random.ints() Random.longs() Random.doubles() (p. 900) | IntStream LongStream DoubleStream | Finite or infinite, depending on parameters | Sequential | Unordered |
CharSequence.chars() (p. 901) | IntStream | Finite | Sequential | Ordered |
String.lines() (p. 902) | Stream<String> | Finite | Sequential | Ordered |
BufferedReader.lines() (p. 902) | Stream<String> | Finite | Sequential | Ordered |
Files.lines() (p. 903) | Stream<String> | Finite | Sequential | Ordered |
16.5 Intermediate Stream Operations
A stream pipeline is composed of stream operations. The stream operations process the elements of the stream to produce some result. After the creation of the initial stream, the elements of the stream are processed by zero or more intermediate operations before the mandatory terminal operation reduces the elements to some final result. The initial stream can undergo several stream transformations (technically called mappings) by the intermediate operations as the elements are processed through the pipeline.
Intermediate operations map a stream to a new stream, and the terminal operation reduces the final stream to some result. Because of the nature of the task they perform, the operations in a stream pipeline are also called map-reduce transformations.
Leave a Reply