fun testMapIndexed() { val listOf = listOf<Int>(3, 2, 3, 5, 4, 1) listOf.mapIndexed { index, result -> println("$index,$result") } } 0,3 1,2 2,3 3,5 4,4 5,1 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. fun testMapIndexed() { val listOf = listOf...
mapLatest:类似于collectLatest,当emit发送新值,会取消掉map上一次转换还未完成的值。 mapNotNull:仅发送map之后不为空的值。 transform:对发出的值进行变换 。不同于map的是,经过transform之后可以重新发送数据,甚至发送多个数据,因为transform内部又重新构建了flow。 transformLatest:类似于mapLatest,当有新值发送时,...
Kotlin 中的集合定义了很多高阶函数,例如 filter、map 等: data class Person(val name: String, val age: Int) fun main(arg: Array<String>) { val person = listOf(Person("Alice", 29), Person("Bob", 31)) println(person.filter { it.age > 30 }) } 上面代码调用了集合的 filter 函数...
* 定义Map Key类型是Int, Value类型是String */ var map1: TreeMap<Int, String> = TreeMap<Int, String>() map1[0] = "Java语言" map1[1] = "Kotlin语言" map1[2] = "C语言" map1[3] = "C++语言" map1[4] = "C#语言" map1[5] = "PHP语言" // 打印Key Value详情 println("keys:...
map 将每一个元素按指定规则变换 any 判断列表中是否有满足谓词条件的元素 all 判断列表中是否所有元素都满足谓词条件 find 找第一个满足谓词条件的元素,如果不存在则为 null,等价于将谓词条件作为参数的 first 或者 firstOrNull count 计算列表中满足谓词条件的元素个数 ...
com.example.fordemo I indexItemFor-index:2 indexItemFor-item:orange kt提供的方式(java可能也有) /** * 同时获取对应角标(索引)和值,类似map吧 * */ fun indexItemFor(dataList: List<String>) { for ((index, item) in dataList.withIndex()) { println("indexItemFor-index:" + index + " ...
flatMapMerge:相当于map + flattenMerge,参数concurrency: Int来限制并发数。 zip:组合两个Flow流最新发出的数据,上游流在同一协程中顺序收集,没有任何缓冲。不同于combine的是,当其中一个流结束时,另外的Flow也会调用cancel,生成的流完成。 lifecycleScope.launch{valflow=flowOf(1,2,3).onEach{ delay(50) }...
// 支持数字创建 val set = hashSetOf(1, 7, 53) // 用类似的方法创建一个 list 或 map: val list = arrayListOf(1, 7, 53) val map = hashMapOf(1 to "one", 7 to "seven, 53 to fifty-three") /**注意: to 并不是一个特殊的结构,而是一个普通函数。后面讨论。*/ // javaClass 相...
https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/map.html 将发出的值 进行变换 ,lambda的返回值为最终发送的值。 flow { emit(1) emit(2) } .map {value-> value*2 } .collect { println(value) ...
for ((index, item) in collection.withIndex()) { //迭代操作 } ``` 这种形式的循环可让我们同时获取索引和元素。 6.遍历Map的for循环 对于Map类型的集合,我们可以使用for循环来遍历键值对。例如: ```kotlin for ((key, value) in map) { //迭代操作 } ``` 在这种形式的循环中,key和value分别代表...