Summary of Terminal Stream Operations – Streams
By Stephen Trude / April 23, 2022 / No Comments / Certifications of Oracle, Consumer Action on Stream Elements
Summary of Terminal Stream Operations
The terminal operations of the Stream<T> class are summarized in Table 16.5. The type parameter declarations have been simplified, where any bound <? super T> or <? extends T> has been replaced by <T>, without impacting the intent of a method. A reference is provided to each method in the first column.
The last column in Table 16.5 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 method allMatch() returns a boolean value to indicate whether all elements of a stream satisfy a given predicate. This predicate is implemented as a functional interface Predicate<T> that is applied to each element in the stream.
The interfaces IntStream, LongStream, and DoubleStream define analogous methods to those shown for the Stream<T> interface in Table 16.5. Methods that are only defined by the numeric stream interfaces are shown in Table 16.6.
Table 16.5 Terminal Stream Operations
Method name (ref.) | Any type parameter + return type | Functional interface parameters | Function type of parameters |
forEach (p. 948) | void | (Consumer<T> action) | T -> void |
forEachOrdered (p. 948) | void | (Consumer<T> action) | T -> void |
allMatch (p. 949) | boolean | (Predicate<T> predicate) | T -> boolean |
anyMatch (p. 949) | boolean | (Predicate<T> predicate) | T -> boolean |
noneMatch (p. 949) | boolean | (Predicate<T> predicate) | T -> boolean |
findAny (p. 952) | Optional<T> | () | |
findFirst (p. 952) | Optional<T> | () | |
count (p. 953) | long | () | |
max (p. 954) | Optional<T> | (Comparator<T> cmp) | (T,T) -> int |
min (p. 954) | Optional<T> | (Comparator<T> cmp) | (T,T) -> int |
reduce (p. 955) | Optional<T> | (BinaryOperator<T> accumulator) | (T,T) -> T |
reduce (p. 955) | T | (T identity, BinaryOperator<T> accumulator) | T -> T, (T,T) -> T |
reduce (p. 955) | <U> U | (U identity, BiFunction<U,T,U> accumulator, BinaryOperator<U> combiner) | U -> U, (U,T) -> U, (U,U) -> U |
collect (p. 964) | <R,A> R | (Collector<T,A,R> collector) | Parameter is not a functional interface. |
collect (p. 964) | <R> R | (Supplier<R> supplier, BiConsumer<R,T> accumulator, BiConsumer<R,R> combiner) | () -> R, (R,T) -> void, (R,R) -> void |
toArray (p. 971) | Object[] | () | |
toArray (p. 971) | <A> A[] | (IntFunction<A[]> generator) | int -> A[] |
toList (p. 972) | List<T> | () |
Table 16.6 Additional Terminal Operations in the Numeric Stream Interfaces
Method name (ref.) | Return type |
average (p. 949) | OptionalNumType, where NumType is Int, Long, or Double |
sum (p. 949) | numtype, where numtype is int, long, or double |
summaryStatistics (p. 974) | NumTypeSummaryStatistics, where NumType is Int, Long, or Double |