Summary of Intermediate Stream Operations – Streams

Summary of Intermediate Stream Operations

Table 16.3 summarizes selected aspects of the intermediate operations.

Table 16.3 Selected Aspects of Intermediate Stream Operations

Intermediate operationStateful/StatelessCan change stream sizeCan change stream typeEncounter order
distinct (p. 915)StatefulYesNoUnchanged
dropWhile (p. 913)StatefulYesNoUnchanged
filter (p. 910)StatelessYesNoUnchanged
flatMap (p. 921)StatelessYesYesNot guaranteed
limit (p. 917)Stateful, short-circuitedYesNoUnchanged
map (p. 921)StatelessNoYesNot guaranteed
mapMulti (p. 927)StatelessYesYesNot guaranteed
parallel (p. 933)StatelessNoNoUnchanged
peek (p. 920)StatelessNoNoUnchanged
sequential (p. 933)StatelessNoNoUnchanged
skip (p. 915)StatefulYesNoUnchanged
sorted (p. 929)StatefulNoNoOrdered
takeWhile (p. 913)Stateful, short-circuitedYesNoUnchanged
unordered (p. 932)StatelessNoNoNot guaranteed

The intermediate operations of the Stream<T> interface (including those inherited from its superinterface BaseStream<T,Stream<T>>) are summarized in Table 16.4. The type parameter declarations have been simplified, where any bounds <? super T> or <? extends T> have been replaced by <T>, without impacting the intent of a method. A reference is provided to each method in the first column. Any type parameter and return type declared by these methods are shown in column two.

The last column in Table 16.4 indicates the function type of the corresponding parameter in the previous column. It is instructive to note how the functional interface parameters provide the parameterized behavior of an operation. For example, the filter() method returns a stream whose elements satisfy a given predicate. This predicate is defined by the functional interface Predicate<T> that is implemented by a lambda expression or a method reference, and applied to each element in the stream.

The interfaces IntStream, LongStream, and DoubleStream also define analogous methods to those shown in Table 16.4, except for the flatMapToNumType() methods, where NumType is either Int, Long, or Double. A summary of additional methods defined by these numeric stream interfaces can be found in Table 16.2.

Table 16.4 Intermediate Stream Operations

Method nameAny type parameter + return typeFunctional interface parametersFunction type of parameters
distinct (p. 915)Stream<T>() 
dropWhile (p. 913)Stream<T>(Predicate<T> predicate)T -> boolean
filter (p. 910)Stream<T>(Predicate<T> predicate)T -> boolean
flatMap (p. 921)<R> Stream<R>(Function<T,Stream<R>> mapper)T -> Stream<R>
flatMapToDouble (p. 921)DoubleStream(Function<T,DoubleStream> mapper)T -> DoubleStream
flatMapToInt (p. 921)IntStream(Function<T,IntStream> mapper)T -> IntStream
flatMapToLong (p. 921)LongStream(Function<T,LongStream> mapper)T -> LongStream
limit (p. 917)Stream<T>(long maxSize) 
map (p. 921)<R> Stream<R>(Function<T,R> mapper)T -> R
mapMulti (p. 927)<R> Stream<R>(BiConsumer<T,Consumer<R>> mapper)(T, Consumer<R>) -> void
mapToDouble (p. 921)DoubleStream(ToDoubleFunction<T> mapper)T -> double
mapToInt (p. 921)IntStream(ToIntFunction<T> mapper)T -> int
mapToLong (p. 921)LongStream(ToLongFunction<T> mapper)T -> long
parallel (p. 933)Stream<T>() 
peek (p. 920)Stream<T>(Consumer<T> action)T -> void
sequential (p. 933)Stream<T>() 
skip (p. 915)Stream<T>(long n) 
sorted (p. 929)Stream<T>() 
sorted (p. 929)Stream<T>(Comparator<T> cmp)(T,T) -> int
takeWhile (p. 913)Stream<T>(Predicate<T> predicate)T -> boolean
unordered (p. 932)Stream<T>() 

Leave a Reply

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