Sub topic 13
Processing Arrays with Streams
A stream is a way to process a sequence of values. A Java array can provide those values to a stream. You can then select values, change them, or combine them into one result.
Streams are useful when you want to describe what to do with array values without writing an indexed loop. A stream does not store the array’s values or change the array by itself.
Build a stream pipeline
A pipeline is a series of processing steps. For example, this pipeline takes scores from an array, keeps scores of at least 80, adds five points to each, and finds their total.
The steps use these methods:
Arrays.stream(scores)creates a stream from the array.filterkeeps values that meet a condition.mapturns each kept value into another value.sumadds the resulting values.
The filter and map steps use a lambda. A lambda is a short way to describe an action. In score -> score + 5, score is the current value, and the part after -> describes the result.
Try a complete example
Save this program as ArrayStreamsDemo.java, then compile and run it.
import java.util.Arrays;
public class ArrayStreamsDemo {
public static void main(String[] args) {
int[] scores = {80, 65, 90, 100};
int total = Arrays.stream(scores)
.filter(score -> score >= 80)
.map(score -> score + 5)
.sum();
int[] passingScores = Arrays.stream(scores)
.filter(score -> score >= 80)
.sorted()
.toArray();
System.out.println("Bonus total: " + total);
System.out.println("Passing scores: " + Arrays.toString(passingScores));
System.out.println("Original: " + Arrays.toString(scores));
}
}The output is:
Bonus total: 285
Passing scores: [80, 90, 100]
Original: [80, 65, 90, 100]For the total, the filter keeps 80, 90, and 100. The map step changes those stream values to 85, 95, and 105. Their sum is 285.
The second pipeline uses sorted to put the kept values in order. Its toArray method creates a new array from those values. Neither pipeline changes scores, so the original still includes 65.
Know when processing happens
Methods such as filter, map, and sorted add steps to a pipeline. They do not process the values on their own. The work starts when you call a method that produces a result, such as sum or toArray. Such a method is called a terminal operation.
A stream can be used for only one terminal operation. That is why the example calls Arrays.stream(scores) twice: once for the total and once for the new array.
Arrays.stream also works with arrays of objects, such as a String[]. The available result methods depend on the kind of values in the stream.
A stream is not always faster than a loop. Choose it when its steps make the task clear.
Exercise
With scores {70, 80, 95}, what does a stream produce if it filters for scores of at least 80, adds 5 to each kept score, and calls sum?
Show answer
185. It keeps 80 and 95, changes them to 85 and 100, and adds them.
Exercise
After calling sum on a stream, how can you process the same array again?
Show answer
Create a fresh stream from the array with Arrays.stream.