Member-only story
Kotlin Flow — Combine, Merge and Zip
Exploring the Power of Kotlin Flow: Combining, Merging, and Zipping Streams
3 min readMay 20, 2023
This is part of the asynchronous flow series:
- Part 1 — Exploring Android LiveData Usages and Behaviors
- Part 2 — Introduction to Kotlin Flows and Channels
- Part 3 — Exploring Different Ways to Collect Kotlin Flow
- Part 4 — Convert Flow to SharedFlow and StateFlow
- Part 5 — Flow, SharedFlow, StateFlow Class Diagram
- Part 6 — Kotlin Flow — Combine, Merge and Zip
I have 2 flows here.
Flow1 emits int
starting from 1 -> 1000 every 1 second.
private val flow1: Flow<Int> = flow {
repeat(10000) { value ->
delay(1000)
Log.d(tag, "[Flow1]: emitting $value")
emit(value)
}
}
Flow 2 emits char
from A to Z every 2 seconds.
private val flow2: Flow<Char> = flow {
var value = 'A'
while(true) {
delay(2000)
Log.d(tag, "[Flow2]: emitting $value")
emit(value)
if (value == 'Z') {
value = 'A'
} else {
value += 1
}
}
}