Kotlin Flow - Combine, Merge and Zip
Tags: kotlin, android, beginners, flow
Published: 2023-05-20 00:00:00

**Exploring the Power of Kotlin Flow: Combining, Merging, and Zipping Streams**

This is part of the asynchronous flow series:

* [Part 1 - Exploring Android LiveData Usages and Behaviors](https://vtsen.hashnode.dev/exploring-android-livedata-usages-and-behaviors)
    
* [Part 2 - Introduction to Kotlin Flows and Channels](https://vtsen.hashnode.dev/introduction-to-kotlin-flows-and-channels)
    
* [Part 3 - Exploring Different Ways to Collect Kotlin Flow](https://vtsen.hashnode.dev/exploring-different-ways-to-collect-kotlin-flow)
    
* [Part 4 - Convert Flow to SharedFlow and StateFlow](https://vtsen.hashnode.dev/convert-flow-to-sharedflow-and-stateflow)
    
* [Part 5 - Flow, SharedFlow, StateFlow Class Diagram](https://vtsen.hashnode.dev/flow-sharedflow-stateflow-class-diagram)
    
* **Part 6 - Kotlin Flow - Combine, Merge and Zip**
    

I have 2 flows here.

Flow1 emits `int` starting from **1 -&gt; 1000** every **1 second**.

```kotlin
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.**

```kotlin
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
        }
    }
}
```

## Combine Flow

This combines **flow1** and **flow2** into **combineFlow** using the `Flow.combine()` [extension function](https://vtsen.hashnode.dev/complete-c-to-kotlin-syntax-comparisons#heading-extension-methods-functions) flow operator.

```kotlin
val combineFlow = flow1.combine(flow2) { flow1Value,flow2Value  ->
    "${flow1Value}_${flow2Value}"
}
```

> The limitation of this `Flow.combine()` extension function is it is **limited to combining 2 flows**. To combine more than 2 flows, you can use `combine()` function directly which supports up to 5 flows.

```kotlin
val combineFlow = combine(flow1, flow2) { flow1Value, flow2Value ->
    "${flow1Value}_${flow2Value}"
}
```

To collect the flow, I use the [LaunchedEffect()](https://vtsen.hashnode.dev/side-effects-summary-in-jetpack-compose#heading-suspended-effect-handler) side effect for this demonstration purpose. The recommended way is either using `collectAsStateWithLifeCylce()`or `repeatOnLifecycle(Lifecycle.State.STARTED)`, see [here](https://vtsen.hashnode.dev/exploring-different-ways-to-collect-kotlin-flow#heading-3-collect-flow-using-lifecyclerepeatonlifecycle).

```kotlin
LaunchedEffect(true) {
    viewModel.combineFlow.collect { value ->
        Log.d(tag, "[Combine Flow]: $value")
    }
}
```

So this is the output. It **combines** the **latest value** from **flow1** and **flow2**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681814269958/c8ad6950-abc5-4bca-810b-f30e32a9c59d.png)

## Merge Flow

This merges **flow1** and **flow2** into **mergeFlow,**

```kotlin
val mergeFlow = merge(flow1, flow2)
```

and here is the output:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681814693024/33aaa699-07d6-46e3-af00-16e8ff266cbc.png)

`1` and `A` are emitted at the same time, followed by `2`, then `3` and `B` and so on.

## Zip Flow

This zips **flow1** and **flow2** into **zipFlow,**

```kotlin
val zipFlow = flow1.zip(flow2) { flow1value,flow2value  ->
    "${flow1value}_${flow2value}"
}
```

and here is the output:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681814999953/dc8d3706-7d20-4691-bce2-5323f1583a82.png)

As you can see from this output, **Zip pairs up the data** from **flow1** and **flow2**.

## Conclusion

The above diagrams help me to understand the difference between combine, merge and zip flow operators.

I have been using `combine()` in this project [here](https://github.com/vinchamp77/AndroidNews) which combines the flow from a ROOM database(articles) with another flow from a Proto DataStore(user settings). I do not have any chance to use `Merge()` and `Zip()`. :)

## Source Code

GitHub Repository: [**Demo\_AsyncFlow**](https://github.com/vinchamp77/Demo_AsyncFlow) (see the `CombineMergeZipFlowActivity`)

---

*Originally published at [https://vtsen.hashnode.dev](https://vtsen.hashnode.dev/kotlin-flow-combine-merge-and-zip)*.