Kotlin filterIsInstance filterNotNull forEach

Kotlin filterIsInstance filterNotNull forEach

Kotlin 复制代码
fun main(args: Array<String>) {
    val i1 = MyItem(1, 1)
    val i2: MyItem? = null
    val i3: Int = 3
    val i4 = "4"
    val i5 = null
    val i6 = MyItem(6, 6)

    val list = mutableListOf<Any?>(i1, i2, i3, i4, i5, i6)
    list.filterIsInstance(MyItem::class.java).forEach {
        println(it)
    }

    println("---")

    list.filterNotNull().forEach {
        println(it)
    }
}

class MyItem {
    private var id = -1
    private var pos = -1

    constructor(id: Int, pos: Int) {
        this.id = id
        this.pos = pos
    }

    override fun toString(): String {
        return "id=${id} pos=${pos}"
    }
}

id=1 pos=1

id=6 pos=6


id=1 pos=1

3

4

id=6 pos=6

Kotlin HashMap entries.filter过滤forEach_kotlin map.entries.foreach-CSDN博客文章浏览阅读997次。一、flow ,emit,onCompletion,collect。四、map,重组改写数据。八、conflate 合并。九、debounce去重。二、函数作为flow。kotlin协程flow filter map flowOn zip combine(1)_zhangphil的博客-CSDN博客。_kotlin map.entries.foreachhttps://blog.csdn.net/zhangphil/article/details/133990762

相关推荐
zh_xuan1 天前
kotlin with函数
开发语言·kotlin
zh_xuan1 天前
kotlin let函数
开发语言·kotlin
Kapaseker1 天前
Kotlin 协程的取消,我觉得设计的不好
android·kotlin
zh_xuan1 天前
kotlin 作用域函数run
开发语言·kotlin
Libraeking1 天前
04 跨越边界:如何将 Android 本地能力暴露给 AI(MCP + Kotlin)
android·人工智能·kotlin
zh_xuan1 天前
kotlin 作用域函数also
开发语言·kotlin
zh_xuan1 天前
kotlin 作用域函数apply
开发语言·kotlin·作用域函数
DeepModel2 天前
【回归算法】决策树回归超详细讲解
决策树·回归·kotlin
zh_xuan2 天前
kotlin Flow的用法2
android·开发语言·kotlin·协程·flow·被压
zh_xuan2 天前
kotlin 测试协程嵌套
android·kotlin·协程