Java 8 Streams API - Terminal Operations We have been discussing the Java 8 Streams API since couple of our posts. We have had an overview of the Java 8 Streams API, the Laziness and the Performance improvements it brings, and the Intermediate Operations it provides. Today, we are going to cover various Terminal Operations provided by the streams api. An usual stream operation flow can have a pipe of multiple intermediate operations and a terminal operation at the end. The intermediate operations are called upon streams and there return type is stream. Hence,
Conditional Matching and Finding
Finding:
Java 8 Streams API provides two methods for the finding purpose- findAny and findFirst.
Reducing
The reducing has a capability of processing the elements in a stream repeatedly to produce an output in the form of a single element. Reducing reduces the entire stream into a single value.
The reduce function accepts an identity or a starting value of the output, and then it combines the identity with the first element of the stream, the result is then combined with the second element of the stream and so on. The logic, how the elements are combined together, is provided as an accumulator.
Min and Max:
Optional<integer> min = numbers.stream() .reduce(0, Integer::min);
The Java 8 Streams reduce method, when called upon parallel streams, internally hides this jargon, and we simply do not need to be worried about. In such cases the reduce makes use of Map Reduce to perform the calculations. The stream is partitioned into pieces and all of the pieces are processed in parallel. Each processing unit will have it's own resulting variable, and hence there is no concern of shared state. All of these results are then combined together to get the final result.
Optional
Java 8 comes up with a special class which helps solving this problem. The Optional class represents whether an object is assigned or unassigned (Null).
Read full article from amitph.com > Java Tutorials: Java 8 Streams API - Terminal Operations
Conditional Matching and Finding
Finding:
Java 8 Streams API provides two methods for the finding purpose- findAny and findFirst.
Reducing
The reducing has a capability of processing the elements in a stream repeatedly to produce an output in the form of a single element. Reducing reduces the entire stream into a single value.
The reduce function accepts an identity or a starting value of the output, and then it combines the identity with the first element of the stream, the result is then combined with the second element of the stream and so on. The logic, how the elements are combined together, is provided as an accumulator.
Min and Max:
Optional<integer> min = numbers.stream() .reduce(0, Integer::min);
The Java 8 Streams reduce method, when called upon parallel streams, internally hides this jargon, and we simply do not need to be worried about. In such cases the reduce makes use of Map Reduce to perform the calculations. The stream is partitioned into pieces and all of the pieces are processed in parallel. Each processing unit will have it's own resulting variable, and hence there is no concern of shared state. All of these results are then combined together to get the final result.
Optional
Java 8 comes up with a special class which helps solving this problem. The Optional class represents whether an object is assigned or unassigned (Null).
Read full article from amitph.com > Java Tutorials: Java 8 Streams API - Terminal Operations
No comments:
Post a Comment