Skip to content

Collectors API

The Collectors class provides common reduction operations for Stream.collect().

fpstreams.Collectors

Common implementations of Collector operations.

grouping_by(classifier, downstream=None) staticmethod

Groups elements by a classification function.

Parameters:

Name Type Description Default
classifier Callable[[T], K]

Function to determine the key.

required
downstream Callable[[Iterable[T]], R] | None

Optional collector to apply to the values of each group. If None, defaults to to_list().

None
Example

grouping_by(lambda x: len(x), downstream=Collectors.counting())

summarizing(mapper) staticmethod

Returns a SummaryStatistics object (count, sum, min, average, max).

counting() staticmethod

Counts the elements.

summing(mapper) staticmethod

Sums the elements after mapping them.

averaging(mapper) staticmethod

Calculates the average.

mapping(mapper, downstream) staticmethod

Adapts a collector to accept elements of a different type. Useful inside grouping_by.

Example

grouping_by(user.department, mapping(user.salary, averaging()))

to_dict(key_mapper, value_mapper) staticmethod

Collects elements into a Dictionary. Example: Stream(users).collect(to_dict(lambda u: u.id, lambda u: u.name))

partitioning_by(predicate) staticmethod

Partitions elements into two lists based on a predicate. Returns a dict: {True: [matches], False: [non-matches]}

to_columns() staticmethod

Transposes a list of dicts into a dict of lists. Handles missing keys by inserting None to ensure alignment.