What does flowOf(...) do, and how does it differ from a list of values?
answer
- flowOf = shorthand for flow { emit(...) }
- Cold: re-emits on every collect
- flowOf() with no args = emptyFlow()
- List = eager memory; Flow = lazy stream
- collect is suspend
basics
~10 sflowOf takes a fixed set of values and wraps them in a Flow, emitting each one in order when you collect it. A list just holds values; a Flow streams them on demand.
solid answer
~30 sflowOf(a, b, c) is a convenience cold-flow builder that emits the given fixed values in order, then completes. It is essentially shorthand for flow { emit(a); emit(b); emit(c) }. Being cold, nothing runs until a terminal operator like collect() is called, and the values are re-emitted on every collection. Unlike a List (an in-memory eager collection), a Flow is a lazy stream you observe with suspending collectors, so it composes with operators (map, filter), runs inside a coroutine, and supports backpressure via suspension. There is also a zero-arg flowOf() that produces an empty flow, equivalent to emptyFlow().
code
kotlin · 7 linesimport kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking
fun main() = runBlocking {
val nums = flowOf(10, 20, 30)
nums.collect { println(it) } // 10 20 30
}go deeper
Knows flowOf emits fixed values when collected and that a flow is lazy, unlike a list.
Explains cold semantics, equivalence to flow { emit }, and the empty/single-arg variants.
Discusses where flowOf fits in a pipeline, backpressure via suspension, and when asFlow is preferable.
Frames cold builders within the broader reactive/coldness model and trade-offs vs hot SharedFlow seeds.
## What flowOf is `flowOf` is one of Kotlin's **convenience cold-flow builders** in `kotlinx.coroutines.flow`. It takes a fixed, known set of values and returns a `Flow<T>` that emits them in order. ```kotlin val flow: Flow<Int> = flowOf(1, 2, 3) ``` This is equivalent to writing the more verbose `flow { }` builder: ```kotlin val flow = flow { emit(1) emit(2) emit(3) } ``` ## Cold means lazy **Cold** means the builder does nothing until a **terminal operator** (such as `collect`) runs. Each collection re-executes the emission from scratch: ```kotlin val f = flowOf(1, 2, 3) f.collect { print(it) } // prints 123 f.collect { print(it) } // prints 123 again ``` ## Flow vs List - A `List<Int>` is an **eager, in-memory collection** — all elements exist at once. - A `Flow<Int>` is a **lazy asynchronous stream** — values are produced one at a time when collected. - Collecting a flow happens inside a **coroutine**; `collect` is a `suspend` function. - Flows compose with operators like `map`, `filter`, `onEach`; suspension provides natural **backpressure**. ## Empty and single variants - `flowOf()` (no args) returns an empty flow — same as `emptyFlow<T>()`. - `flowOf(x)` (one arg) is a single-value flow. ## When to use it Use `flowOf` for **fixed, literal values** you want to feed into a flow pipeline — for tests, defaults, or seeding a chain of operators. For values you already hold in a collection, prefer `asFlow()` instead.
- What does flowOf() with no arguments produce?An empty flow that completes immediately without emitting anything, equivalent to emptyFlow().
- If you collect the same flowOf twice, what happens?It re-emits all values both times, because flows are cold and each collection restarts the emission.
flowOf is like a player piano roll: the notes are fixed, but they only play when someone presses start.
saying these in an interview costs you the question
- Calling a Flow 'just a list with extra steps'
- Thinking flowOf runs immediately when constructed
- Believing the values are emitted only once across multiple collectors
- Confusing flowOf with a hot stream like StateFlow