Flow 运算符:map、filter、transform 与 take
应用中间运算符来转换和限制 Flow 的发送值。
Flow 运算符:map、filter、transform 与 take 是 CoddyKit 上的免费 Kotlin Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Kotlin Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Kotlin Academy 课程共包含 4 节课。
什么是流
Flow 是一种冷的异步数据流。map、filter 和 transform 等中间操作符都是惰性的——只有在收集时才会运行。
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun numbers(): Flow<Int> = flow {
repeat(5) { emit(it + 1) }
}
fun main() = runBlocking {
numbers().collect { println(it) }
}映射操作符
map 会转换每个发出的值。这是一种一对一的惰性转换。
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun main() = runBlocking {
(1..5).asFlow()
.map { it * it }
.collect { println(it) } // 1 4 9 16 25
}过滤操作符
filter 只会传递符合谓词的元素,并丢弃其余元素。
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun main() = runBlocking {
(1..10).asFlow()
.filter { it % 2 == 0 }
.collect { println(it) } // 2 4 6 8 10
}转换操作符
transform 是最通用的操作符:对于每个输入元素,您可以发出零个、一个或多个值。
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun main() = runBlocking {
(1..3).asFlow()
.transform { n ->
emit("Item $n")
emit("Done $n")
}
.collect { println(it) }
}take 操作符
take(n) 只获取前 n 个元素,然后取消上游流。
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun main() = runBlocking {
flow {
repeat(100) { emit(it) }
}.take(3)
.collect { println(it) } // 0 1 2
}takeWhile 与 dropWhile
takeWhile 会在谓词为真时获取元素;dropWhile 会一直丢弃元素,直到谓词变为假。
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun main() = runBlocking {
(1..10).asFlow()
.takeWhile { it < 5 }
.collect { println(it) } // 1 2 3 4
(1..10).asFlow()
.dropWhile { it < 5 }
.collect { println(it) } // 5 6 7 8 9 10
}串联操作符
操作符可以组合:每个操作符都会返回一个新的 Flow,从而支持声明式处理流水线。
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun main() = runBlocking {
(1..20).asFlow()
.filter { it % 3 == 0 }
.map { it * 2 }
.take(4)
.collect { println(it) } // 6 12 18 24
}mapNotNull
mapNotNull 会在一次操作中完成映射并丢弃空结果——等同于 map + filter { it != null }。
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun main() = runBlocking {
flowOf("1", "two", "3", "four", "5")
.mapNotNull { it.toIntOrNull() }
.collect { println(it) } // 1 3 5
}flatMapConcat
flatMapConcat 会将每个元素映射为一个流,并按顺序串联结果。
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun main() = runBlocking {
(1..3).asFlow()
.flatMapConcat { n -> flowOf("$n-a", "$n-b") }
.collect { println(it) }
}使用 onEach 执行副作用
onEach 会对每个元素执行副作用,而不改变数据流——适合在处理流水线中记录日志或分析数据。
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun main() = runBlocking {
(1..5).asFlow()
.onEach { println("Processing: $it") }
.filter { it % 2 == 0 }
.collect { println("Collected: $it") }
}流是冷的
每次调用 collect 都会从头重新启动该流。在终结操作符触发收集之前,不会发出或转换任何元素。
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun main() = runBlocking {
val flow = (1..3).asFlow().map { println("map $it"); it * 2 }
println("Before collect")
flow.collect { println("got $it") }
println("Second collect:")
flow.collect { println("got $it") } // runs again
}快速检查
哪个操作符会为每个上游元素发出零个、一个或多个值?
回顾
映射执行一对一转换;过滤丢弃元素;转换可以发出任意数量的值;take限制数量。请以声明式方式串联它们——流是冷的,只有在收集时才会运行。
用 AI 导师学习 Kotlin — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 51
- 课程
- 203
常见问题解答
「Flow 运算符:map、filter、transform 与 take」课时是免费的吗?
是的 — 「Flow 运算符:map、filter、transform 与 take」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Kotlin Academy 课程的其余内容,请升级到 CoddyKit PRO。 Kotlin Academy 课程共包含 4 节课。
「Flow 运算符:map、filter、transform 与 take」这节课中我会学到什么?
应用中间运算符来转换和限制 Flow 的发送值。 你通过在浏览器中直接运行的动手代码来练习 Kotlin Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Kotlin Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Kotlin Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「Flow 运算符:map、filter、transform 与 take」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Kotlin Academy 课中编写并运行代码吗?
能。每节 Kotlin Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Flow 运算符:map、filter、transform 与 take
- catch 与 onCompletion:Flow 中的错误处理
- combine 与 zip:合并多个 Flow
- flowOn 与 buffer:上下文和背压