Search⌘ K
AI Features

Hot vs. Cold

Explore the distinctions between hot and cold data streams in Kotlin coroutines. Understand how hot streams produce data eagerly and store elements, while cold streams operate lazily on demand without storing data. Learn through examples how lists and channels represent hot streams and sequences or flows represent cold streams. Gain insights into the practical implications for memory use, operation efficiency, and stream processing in Kotlin.

Overview

Kotlin coroutines initially only had channels support, but creators noticed this wasn’t enough. Channels are a hot stream of values, but we often need a stream that’s cold.

Understanding the difference between hot and cold streams of data is useful software-craftsmanship knowledge because most data sources we use daily fall into one of these two categories. Collections (List, Set, etc.) are hot, while Sequence and Java Stream are cold. A Channel is hot, while Flow and RxJava streams (Observable, Single, etc.) are cold.

Note: This is true in general, but there are exceptions. Some functions and builders, like buffer or channelFlow ...