Kotlin Flow常用用法

[复制链接]
发表于 2025-10-20 20:44:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
Kotlin Flow 是 Kotlin 编程语言中的一个强盛特性,用于处置惩罚异步数据流。它是 Kotlin 协程库的一部分,旨在以声明式和相应式的方式处置惩罚异步数据流。Flow 的筹划与协程无缝集成,使得异步编程更加简朴和直观。
  1. suspend fun main() {
  2.     // 创建Flow的几种方法
  3.     val flow = flow {
  4.         emit(1)
  5.         emit(2)
  6.         emit(3)
  7.         emit(4)
  8.         emit(5)
  9.     }.collect {
  10.         println(it) // 打印1 2 3 4 5 共5行
  11.     }
  12.     val flow2 = flowOf(1, 2, 3, 4, 5)
  13.     flow2.collect {
  14.         println(it) // 打印1 2 3 4 5 共5行
  15.     }
  16.     val flow3 = listOf(1, 2, 3).asFlow();
  17.     flow3.collect {
  18.         println(it) // 打印1 2 3 共3行
  19.     }
  20.     val flow4 = channelFlow {
  21.         send(1)
  22.         send(2)
  23.         send(3)
  24.         send(4)
  25.         send(5)
  26.     }
  27.     flow4.collect {
  28.         println(it) // 打印1 2 3 4 5 共5行
  29.     }
  30.     // 获取第一个和最后一个元素
  31.     val firstValue = flow4.first()
  32.     println(firstValue) // 打印1
  33.     val lastValue = flow4.last()
  34.     println(lastValue) // 打印5
  35.     //3.Flow的操作符
  36.     // 3.1 map 操作符 将Flow中的每个元素进行转换
  37.     val flow5 = flowOf(1, 2, 3, 4, 5)
  38.     flow5.map {
  39.         it * 3  //无效
  40.         it * 5  //无效
  41.         it * 10//  有效以最后一行为准注意与trans的区别
  42.     }.collect {
  43.         println(it) // 打印10 20 30 40 50 共5行
  44.     }
  45.     // 3.2 trans 操作符 将Flow中的每个元素进行转换
  46.     val flow6 = flowOf(1, 2, 3)
  47.     flow6.transform {
  48.         emit(it * 3)  //有效
  49.         emit(it * 5)  //有效
  50.     }.collect {
  51.         println(it) // 打印3 5 6 10 9 15 共6行 注意与map的区别
  52.     }
  53.     // 3.3 filter 操作符 过滤Flow中符合条件的元素
  54.     val flow7 = flowOf(1, 2, 3, 4, 5, 6)
  55.     flow7.filter {
  56.         it % 3 == 0
  57.     }.collect {
  58.         println(it) //打印 3 6 共2行
  59.     }
  60.     // take 操作符 取Flow中的前n个元素
  61.     val flow8 = flowOf(1, 2, 3, 4, 5, 6)
  62.     flow8.take(3).collect {
  63.         println(it) //打印 1 2 3 共3行
  64.     }
  65.     //3.4 组合操作符 zip 将2个Flow的值按顺序组合
  66.     val flow9 = flowOf(1, 2, 3)
  67.     val flow10 = flowOf("a", "b", "c")
  68.     flow9.zip(flow10) { it1, it2 ->
  69.         "$it1$it2" //将两个Flow的值按顺序组合
  70.     }.collect {
  71.         println(it) //打印 1a 2b 3c 共3行
  72.     }
  73.     println("=================================")
  74.     //3.5 组合操作符 combine 将多个 Flow 的最新值组合,有可能不是按顺序,比如:
  75.     // 1a
  76.     //2b
  77.     //2c
  78.     //3c
  79.     //使用runBlocking运行
  80.     runBlocking {
  81.         val flow11 = flowOf(1, 2, 3)
  82.         val flow12 = flowOf("a", "b", "c")
  83.         flow11.combine(flow12) { it1, it2 ->
  84.             "$it1$it2" //将两个Flow的值按顺序组合
  85.         }.collect {
  86.             println(it) //打印 1a 2b 3c 共3行
  87.         }
  88.     }
  89.     //3.6 flatMapConcat 扁平化操作符 按顺序将每个值转换为 Flow 并连接
  90.     val flow13 = flowOf(1, 2, 3)
  91.     flow13.flatMapConcat { it -> flowOf(it, it * 2, it * 3) }.collect {
  92.         println(it) //打印 1 2 3 2 4 6 3 6 9 共9行 与transform有点类似
  93.     }
  94.     //3.7 flatMapMerge 扁平化操作符 并发将每个值转换为 Flow 并连接
  95.     val flow14 = flowOf(1, 2, 3)
  96.     flow14.flatMapMerge { it -> flowOf(it, it * 2, it * 3) }.collect {
  97.         println(it) //打印 1 2 3 2 4 6 3 6 9 或者  369246123共9行 与transform有点类似
  98.     }
  99.     //3.8 flatMapLatest 扁平化操作符 只处理最新的值,取消之前的转换
  100.     val flow15 = flowOf(1, 2, 3)
  101.     flow15.flatMapLatest { it -> flowOf(it, it * 2, it * 3) }.collect {
  102.         println(it) // 打印 1 2 3 2 4 6 3 6 9
  103.     }
  104.     //3.9 flatMapLatest 扁平化操作符 只处理最新的值,取消之前的转换 ?
  105.     val flow16 = flowOf(1, 2, 3)
  106.     flow16.flatMapLatest { it -> flowOf(it, it * 2, it * 3) }.collect {
  107.         println(it) //
  108.     }
  109.     // 4. Flow 的冷流与热流
  110. //    4.1 冷流(Cold Stream)
  111. //    Flow 是冷流,只有在被收集时才会执行。
  112. //    每次收集都会重新执行 Flow 的代码
  113. //    示例:
  114.     val flow17 = flow {
  115.         println("Flow start...")
  116.         emit(1)
  117.         emit(2)
  118.     }
  119.     flow17.collect {
  120.         println("Collect : $it") // Flow start...  Collect : 1  Collect : 2 共三行
  121.     }
  122.     flow17.collect {
  123.         println("Collect : $it")  // Flow start...  Collect : 1  Collect : 2 共三行
  124.     }
  125.     // 4.2 热流(Hot Stream)
  126. // Kotlin 中的 StateFlow  是热流的实现
  127. //    热流是预先启动的,可以在收集之前就开始执行。
  128. //    热流可以被多个收集者共享。
  129. //    示例:
  130.     val stateFlow = MutableStateFlow(0)
  131.     stateFlow.value = 1
  132.     stateFlow.value = 2
  133.     stateFlow.value = 3
  134.     stateFlow.collect {
  135.         println("Collect : $it") // Collect : 3
  136.     }
  137. }
复制代码
//  模仿网络哀求
  1. suspend fun main() {
  2.     //  模拟网络请求
  3.     fun requestNetwork(): Flow<Int> = flow {
  4.         repeat(3) {
  5.             delay(1000)
  6.             emit(it)
  7.         }
  8.     }
  9.         requestNetwork().collect { println(it) } //打印 0 1 2 共3行
  10. }
复制代码
总结
Kotlin Flow 是一个强盛的工具,用于处置惩罚异步数据流。它的核心上风包罗:
冷流模子:按需实行,节省资源。
丰富的操纵符:支持复杂的数据流处置惩罚。
与协程无缝集成:简化异步编程。
通过 Flow,你可以以声明式的方式编写异步代码,同时享受 Kotlin 协程带来的简便性和高效性。假如你必要处置惩罚连续的数据流,Flow 是一个非常值得学习和利用的工具!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告
回复

使用道具 举报

×
登录参与点评抽奖,加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表