Java Stream terminal operations¶
In the Java Stream API, terminal operations are the methods that trigger the processing of the stream pipeline. Unlike intermediate operations (which return a new Stream and are lazy), terminal operations produce a result or a side-effect and consume the stream, meaning the stream cannot be used again after a terminal operation is executed.^[600-developer__java__java8__java8.md]
Reduction and Calculation Operations¶
These methods process the elements of the stream to produce a single aggregated value or a summary result.
reduce: Performs a reduction on the elements of the stream, using an associative accumulation function. It is used to combine elements into a single result, such as calculating a sum or concatenating strings^[600-developer__java__java8__java8.md]. It can be called with no initial value (returning anOptional), with an initial value, or with an initial value and a combiner function^[600-developer__java__java8__java8.md].count: Returns the number of elements in the stream^[600-developer__java__java8__java8.md].max/min: Finds the maximum or minimum element of the stream according to a providedComparator^[600-developer__java__java8__java8.md].
Collection Operations¶
These operations gather the elements of the stream into a mutable container or a specific data structure. They are typically invoked via the collect method accepting a Collector.
collect: The most flexible terminal operation, which transforms the stream elements into a result container (e.g.,List,Map,Set) using aCollector^[600-developer__java__java8__java8.md].toList: Accumulates elements into aList^[600-developer__java__java8__java8.md].toSet: Accumulates elements into aSet^[600-developer__java__java8__java8.md].toMap/toConcurrentMap: Accumulates elements into aMaporConcurrentHashMapby applying mapping functions to keys and values^[600-developer__java__java8__java8.md].toCollection: Accumulates elements into a specific collection type (e.g.,TreeSet) via a supplier^[600-developer__java__java8__java8.md].joining: Concatenates stream elements (typically strings) into a singleString, with optional delimiters, prefixes, or suffixes^[600-developer__java__java8__java8.md].groupingBy: Groups elements by a classification function. It supports multi-level grouping, downstream collectors (like summing), and mapping^[600-developer__java__java8__java8.md].summingInt/summingLong/summingDouble: Calculates the sum of integer-valued properties^[600-developer__java__java8__java8.md].averagingInt/averagingLong/averagingDouble: Calculates the arithmetic mean of integer-valued properties^[600-developer__java__java8__java8.md].counting: A collector that counts the number of input elements^[600-developer__java__java8__java8.md].maxBy/minBy: Collectors that find the maximum or minimum element based on aComparator^[600-developer__java__java8__java8.md].
Matching and Searching Operations¶
These methods inspect the stream to return an Optional describing an element, or a boolean result based on a predicate.
findFirst: Returns anOptionaldescribing the first element of the stream^[600-developer__java__java8__java8.md].findAny: Returns anOptionaldescribing some element of the stream (useful for parallel streams)^[600-developer__java__java8__java8.md].allMatch: Returnstrueif all elements of the stream match the provided predicate^[600-developer__java__java8__java8.md].anyMatch: Returnstrueif any element of the stream matches the provided predicate^[600-developer__java__java8__java8.md].noneMatch: Returnstrueif no elements of the stream match the provided predicate^[600-developer__java__java8__java8.md].
Iteration Operations¶
forEach: Performs an action for each element of the stream^[600-developer__java__java8__java8.md].forEachOrdered: Performs an action for each element of the stream, guaranteeing to process elements in the encounter order of the stream (if one exists)^[600-developer__java__java8__java8.md].
Related Concepts¶
- [[Java Stream]]
- [[Functional programming]]
- [[Lambda expressions]]
Sources¶
^[600-developer__java__java8__java8.md]