groupBy

fun <T, K, V> Flow<T>.groupBy(bufferSize: Int = Channel.BUFFERED, keySelector: suspend (T) -> K, valueSelector: suspend (T) -> V): Flow<GroupedFlow<K, V>>(source)

Groups the items emitted by the current Flow according to a specified criterion, and emits these grouped items as GroupedFlows.

The emitted GroupedFlow allows only a single FlowCollector during its lifetime and if this FlowCollector cancels before the source terminates, the next emission by the source having the same key will trigger a new GroupedFlow emission.

If the upstream throw an exception, the returned Flow and all active inner GroupedFlows will signal the same exception.

Parameters

bufferSize

bufferSize hints about the number of expected values from each inner GroupedFlow. Should be either a positive channel capacity or one of the constants defined in Channel. If inner GroupedFlow tries to emit more than bufferSize values before it is collected, it will be suspended. See SendChannel.send for details.

keySelector

a function that extracts the key for each item

valueSelector

a function that extracts the return value for each item

See also


fun <T, K> Flow<T>.groupBy(bufferSize: Int = Channel.BUFFERED, keySelector: suspend (T) -> K): Flow<GroupedFlow<K, T>>(source)

Groups the items emitted by the current Flow according to a specified criterion, and emits these grouped items as GroupedFlows.

The emitted GroupedFlow allows only a single FlowCollector during its lifetime and if this FlowCollector cancels before the source terminates, the next emission by the source having the same key will trigger a new GroupedFlow emission.

If the upstream throw an exception, the returned Flow and all active inner GroupedFlows will signal the same exception.

Parameters

bufferSize

bufferSize hints about the number of expected values from each inner GroupedFlow. Should be either a positive channel capacity or one of the constants defined in Channel. If inner GroupedFlow tries to emit more than bufferSize values before it is collected, it will be suspended. See SendChannel.send for details.

keySelector

a function that extracts the key for each item

See also