summarizingDouble<T> static method
Collector<T, InternalDoubleSummaryStatistics, DoubleSummaryStatistics>
summarizingDouble<T>(
- double mapper(
- T
Returns a Collector which applies a double-producing mapping function to each input element, and returns summary statistics for the resulting values.
Example
final stats = transactions.stream()
.collect(Collectors.summarizingDouble((t) => t.amount));
print('Average amount: ${stats.average}');
print('Total amount: ${stats.sum}');
Implementations of Collector that implement various useful reduction operations, such as accumulating elements into collections, summarizing elements according to various criteria, etc.
The following are examples of using the predefined collectors to perform common mutable reduction tasks:
// Accumulate names into a List
final list = people.stream()
.map((p) => p.name)
.collect(Collectors.toList());
// Accumulate names into a Set
final set = people.stream()
.map((p) => p.name)
.collect(Collectors.toSet());
// Convert elements to strings and concatenate them, separated by commas
final joined = things.stream()
.map((t) => t.toString())
.collect(Collectors.joining(', '));
// Compute sum of salaries of employee
final total = employees.stream()
.collect(Collectors.summingInt((e) => e.salary));
// Group employees by department
final byDept = employees.stream()
.collect(Collectors.groupingBy((e) => e.department));
// Compute sum of salaries by department
final totalByDept = employees.stream()
.collect(Collectors.groupingBy(
(e) => e.department,
Collectors.summingInt((e) => e.salary)));
// Partition students into passing and failing
final passingFailing = students.stream()
.collect(Collectors.partitioningBy((s) => s.grade >= 60));
Implementation
static Collector<T, InternalDoubleSummaryStatistics, DoubleSummaryStatistics> summarizingDouble<T>(double Function(T) mapper) {
return Collector<T, InternalDoubleSummaryStatistics, DoubleSummaryStatistics>(
supplier: () => InternalDoubleSummaryStatistics(),
accumulator: (stats, element) => stats.accept(mapper(element)),
combiner: (stats1, stats2) => stats1.combine(stats2),
finisher: (stats) => DoubleSummaryStatistics(
stats.count,
stats.sum,
stats.min,
stats.max,
stats.average,
),
);
}