Collectors class

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));

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

averagingDouble<T>(double mapper(T)) Collector<T, InternalDoubleSummaryStatistics, double>
Returns a Collector that produces the arithmetic mean of double-valued functions applied to the input elements.
averagingInt<T>(int mapper(T)) Collector<T, InternalIntSummaryStatistics, double>
Returns a Collector that produces the arithmetic mean of integer-valued functions applied to the input elements.
groupingBy<T, K>(K classifier(T), [Collector<T, dynamic, dynamic>? downstream]) Collector<T, Map<K, List<T>>, Map<K, List<T>>>
Returns a Collector implementing a "group by" operation on input elements, grouping elements according to a classification function, and returning the results in a Map.
joining<T>([String delimiter = '', String prefix = '', String suffix = '']) Collector<T, StringBuffer, String>
Returns a Collector that concatenates the input elements into a String, in encounter order.
partitioningBy<T>(bool predicate(T), [Collector<T, dynamic, dynamic>? downstream]) Collector<T, Map<bool, List<T>>, Map<bool, List<T>>>
Returns a Collector which partitions the input elements according to a Predicate, and organizes them into a Map with bool keys.
summarizingDouble<T>(double mapper(T)) Collector<T, InternalDoubleSummaryStatistics, DoubleSummaryStatistics>
Returns a Collector which applies a double-producing mapping function to each input element, and returns summary statistics for the resulting values.
summarizingInt<T>(int mapper(T)) Collector<T, InternalIntSummaryStatistics, IntSummaryStatistics>
Returns a Collector which applies an int-producing mapping function to each input element, and returns summary statistics for the resulting values.
summingDouble<T>(double mapper(T)) Collector<T, InternalDoubleSummaryStatistics, double>
Returns a Collector that produces the sum of double-valued functions applied to the input elements.
summingInt<T>(int mapper(T)) Collector<T, InternalIntSummaryStatistics, int>
Returns a Collector that produces the sum of integer-valued functions applied to the input elements.
toList<T>() Collector<T, List<T>, List<T>>
Returns a Collector that accumulates the input elements into a new List.
toMap<T, K, U>(K keyMapper(T), U valueMapper(T), [U mergeFunction(U, U)?]) Collector<T, Map<K, U>, Map<K, U>>
Returns a Collector that accumulates the input elements into a new Map whose keys and values are the result of applying the provided mapping functions to the input elements.
toSet<T>() Collector<T, Set<T>, Set<T>>
Returns a Collector that accumulates the input elements into a new Set.