demultiplexBy

fun <T, K, R> Flow<T>.demultiplexBy(keySelector: (T) -> K?, flowProducer: suspend FlowCollector<R>.(K, Flow<T>) -> Unit): Flow<R>(source)

Demultiplexes the elements of the flow based on a key selector function. Each element in the flow is assigned a key based on the result of the key selector function. Elements with the same key are grouped together and then processed by the flow producer function.

Return

a new flow that emits the results of the flow producer function.

Parameters

keySelector

the function that determines the key for each element in the flow. The key can be of any type.

flowProducer

the function that processes the grouped elements for each key. It takes the key and a flow of elements with the same key as input.