Mastering terminal operations
As we discussed earlier, no streaming happens until the terminal operation executes. This gives the JVM an overall picture of the stream pipeline, thereby enabling efficiencies to be introduced in the background.
A terminal operation can be performed without any intermediate operation but not the other way around. Reductions are a special type of terminal operation where all of the contents of the stream are combined into a single primitive or Object (for example, a Collection).
Table 15.1 represents the terminal operations we will be discussing in this section:
Table 15.1 – Terminal operations
Before we discuss them in turn, a brief discussion regarding the table. Remember, a reduction must look at all elements in the stream and then return a primitive or Object
.
Some of these terminal operations, such as allMatch(Predicate)
, may not look at all of the elements in the stream. For example, let’s say...