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

MethodReturned stream typeFinite/InfiniteSequential/ParallelOrdered/Unordered
Stream.empty() NumTypeStream.empty() (p. 893)Stream<T> NumTypeStreamFiniteSequentialOrdered
Stream.of() Stream.ofNullable() NumTypeStream.of() (p. 893)Stream<T> Stream<T> NumTypeStreamFiniteSequentialOrdered
Stream.generate() NumTypeStream.generate() (p. 895)Stream<T> NumTypeStreamInfiniteSequentialUnordered
Stream.iterate() NumTypeStream.iterate (p. 895)Stream<T> NumTypeStreamInfiniteSequentialOrdered
Stream.concat() NumTypeStream.concat() (p. 895)Stream<T> NumTypeStreamFinite if both finiteParallel if either parallelOrdered if both ordered
Collection.stream() (p. 897)Stream<T>FiniteSequentialOrdered if collection ordered
Collection.parallelStream() (p. 897)Stream<T>FiniteParallelOrdered if collection ordered
Arrays.stream() (p. 898)Stream<T> NumTypeStreamFiniteSequentialOrdered
IntStream.range() IntStream.rangeClosed() LongStream.range() LongStream.rangeClosed() (p. 898)IntStream IntStream LongStream LongStreamFiniteSequentialOrdered
Random.ints() Random.longs() Random.doubles() (p. 900)IntStream LongStream DoubleStreamFinite or infinite, depending on parametersSequentialUnordered
CharSequence.chars() (p. 901)IntStreamFiniteSequentialOrdered
String.lines() (p. 902)Stream<String>FiniteSequentialOrdered
BufferedReader.lines() (p. 902)Stream<String>FiniteSequentialOrdered
Files.lines() (p. 903)Stream<String>FiniteSequentialOrdered

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

Your email address will not be published. Required fields are marked *