summarizingInt<T> static method
Collector<T, InternalIntSummaryStatistics, IntSummaryStatistics>
summarizingInt<T>(
- int mapper(
- T
Returns a Collector which applies an int-producing mapping function to each input element, and returns summary statistics for the resulting values.
Example
final stats = employees.stream()
.collect(Collectors.summarizingInt((e) => e.salary));
print('Average salary: ${stats.average}');
print('Max salary: ${stats.max}');
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, InternalIntSummaryStatistics, IntSummaryStatistics> summarizingInt<T>(int Function(T) mapper) {
return Collector<T, InternalIntSummaryStatistics, IntSummaryStatistics>(
supplier: () => InternalIntSummaryStatistics(),
accumulator: (stats, element) => stats.accept(mapper(element)),
combiner: (stats1, stats2) => stats1.combine(stats2),
finisher: (stats) => IntSummaryStatistics(
stats.count,
stats.sum,
stats.min,
stats.max,
stats.average,
),
);
}