I'm trying to find a value in a list of objects in kotlin, using for it "filter", but I need to return true or false if the value is found, but filter returns me a list of object in the case of match.

t.filter { it.retailerId == value }

?当我在对象列表中找到这个值时,如何返回布尔值?

推荐答案

If you need that the element is exactly one:

t.filter { it.retailerId == value }.size == 1

if not:

t.any { it.retailerId == value }

With foldRight and a break when you found it:

t.foldRight(false) {val, res ->
                if(it.retailerId == value) {
                    return@foldRight true
                } else {
                    res
                }
            }

Kotlin相关问答推荐

在Kotlin中处理结果的高阶函数

如何在使用Kotlin Coroutines时检测和记录何时出现背压

只能在元素区域中点击的Jetpack Compose列

带有Spring Boot和Kotline的可嵌入实体

将带大括号和不带大括号的Lambda值赋给@Composable函数

Spring Boot Kotlin 数据类未使用 REST 控制器中的默认值初始化

Kotlin 协程:flatMapLatest 什么都不发出

如何在 Jetpack Compose 中启动和停止动画

我可以在 Kotlin 中使用接口类型作为构造函数参数吗

如何在 Android 的 Fragment 中使用 setUserVisibleHint

IntentService (kotlin) 的默认构造函数

Kotlin 静态函数:伴生对象,@JvmStatic @JvmField

未在IntelliJ IDEA上运行临时文件

当被Spring代理类访问时,Kotlin实例变量为null

如何暂停kotlin coroutine直到收到通知

如何从kotlin中的类实例化对象

如何在Kotlin中使方法param可变?

Dagger +Kotlin 不注入

Lint 错误:可疑的相等判断:在 Object DiffUtilEquals 中未实现 equals()

Kotlin内联属性的用例是什么?