Summing
The summing collectors perform a functional reduction to produce the sum of the numeric results from applying a numeric-valued function to the input elements.
static <T> Collector<T,?,
NumType> summingNum
Type(
To
NumType
Function<? super T> mapper)
Returns a collector that produces the sum of a numtype-valued function applied to the input elements. If there are no input elements, the result is zero. The result is of NumType.
NumType is Int (but it is Integer when used as a type name), Long, or Double, and the corresponding numtype is int, long, or double.
The collector returned by the Collectors.summingInt() method is used at (1) as a standalone collector to find the total number of tracks on the CDs. The mapper function CD::noOfTracks passed as an argument extracts the number of tracks from each CD on which the functional reduction is performed.
Integer sumTracks = CD.cdList.stream()
.collect(Collectors.summingInt(CD::noOfTracks)); // (1) Standalone collector
System.out.println(sumTracks); // 42
In the pipeline below, the CDs are grouped by musical genre, and the number of tracks on CDs in each group summed by the downstream collector is returned by the Collectors.summingInt() method at (2).
Map<Genre, Integer> grpByGenre = CD.cdList.stream()
.collect(Collectors.groupingBy(
CD::genre,
Collectors.summingInt(CD::noOfTracks))); // (2) Downstream collector
System.out.println(grpByGenre); // {POP=18, JAZZ=24}
System.out.println(grpByGenre.get(Genre.JAZZ)); // 24
The collector Collectors.summingInt() performs effectively the same functional reduction at (3) as the IntStream.sum() terminal operation (p. 973).
int sumTracks2 = CD.cdList.stream() // (3) Stream<CD>
.mapToInt(CD::noOfTracks) // IntStream
.sum();
System.out.println(sumTracks2); // 42
Averaging
The averaging collectors perform a functional reduction to produce the average of the numeric results from applying a numeric-valued function to the input elements.
static <T> Collector<T,?,Double> averaging
NumType
(
To
NumType
Function<? super T> mapper)
Returns a collector that produces the arithmetic mean of a numtype-valued function applied to the input elements. If there are no input elements, the result is zero. The result is of type Double.
NumType is Int, Long, or Double, and the corresponding numtype is int, long, or double.
The collector returned by the Collectors.averagingInt() method is used at (1) as a standalone collector to find the average number of tracks on the CDs. The mapper function CD::noOfTracks passed as an argument extracts the number of tracks from each CD on which the functional reduction is performed.
Double avgNoOfTracks1 = CD.cdList.stream()
.collect(Collectors
.averagingInt(CD::noOfTracks)); // (1) Standalone collector
System.out.println(avgNoOfTracks1); // 8.4
In the pipeline below, the CDs are grouped by musical genre, and the downstream collector Collectors.averagingInt() at (2) calculates the average number of tracks on the CDs in each group.
Map<Genre, Double> grpByGenre = CD.cdList.stream()
.collect(Collectors.groupingBy(
CD::genre,
Collectors.averagingInt(CD::noOfTracks) // (2) Downstream collector
));
System.out.println(grpByGenre); // {POP=9.0, JAZZ=8.0}
System.out.println(grpByGenre.get(Genre.JAZZ)); // 8.0
The collector created by the Collectors.averagingInt() method performs effectively the same functional reduction as the IntStream.average() terminal operation (p. 974) at (3).
Click here to view code image OptionalDouble avgNoOfTracks2 = CD.cdList.stream() // Stream<CD>
.mapToInt(CD::noOfTracks) // IntStream
.average(); // (3)
System.out.println(avgNoOfTracks2.orElse(0.0)); // 8.4