Kotlin Flow | PART 3

Karishma Agrawal
4 min readMay 2, 2023
Photo by Marc Reichelt on Unsplash

You can find prev Parts here:

Composing multiple flows

In Kotlin, it’s common to need to combine or transform multiple flows into a single stream of data. Here are a few examples of how you can compose multiple flows:

  1. zip operator: This operator combines two flows into a single flow of pairs. It emits the first item from each flow, then the second item from each flow, and so on. For example:
val numbers = flowOf(1, 2, 3)
val letters = flowOf("a", "b", "c")

numbers.zip(letters) { number, letter ->
"$number$letter"
}.collect { combined ->
println(combined)
}
// Output: 1a 2b 3c

2. combine operator: This operator combines two or more flows into a single flow that emits a new value each time any of the source flows emit a value. For example:

--

--

Karishma Agrawal

Android Developer @Eventbrite | Wanted to be a writer so I write code now | Reader