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

相关推荐
alexhilton4 小时前
Android车载OS中的Remote Compose
android·kotlin·android jetpack
plainGeekDev14 小时前
广播接收器 → Flow + Lifecycle
android·java·kotlin
plainGeekDev14 小时前
EventBus → SharedFlow
android·java·kotlin
Kapaseker2 天前
学不动了,入门 Compose Styles API
android·kotlin
plainGeekDev3 天前
MVC 写法 → MVVM
android·java·kotlin
plainGeekDev4 天前
单例模式 → object 声明
android·java·kotlin
rocpp5 天前
Android 多语言切换实战:从 Context 到 Android 13 应用语言适配
android·kotlin
黄林晴5 天前
用了这么久 Koin Scope,原来一直都用错了?
android·kotlin
唐青枫6 天前
Kotlin Context Parameters 详解:别再把 Logger、事务和配置层层往下传
kotlin
Coffeeee6 天前
如何使用Glide和Coil加载WebP动图
android·kotlin·glide