【问题标题】:Kotlin Map with If statement带有 If 语句的 Kotlin 映射
【发布时间】:2020-12-01 13:08:25
【问题描述】:

尝试编写一些函数式代码并使用映射 - 也可能是过滤器? - 替换类似:

for (something in somethingList) {
    if (something.value == someOtherThing.value)
        result = true
} 

在这里找到更实用的东西:https://grokonez.com/kotlin/kotlin-filter-map-examples

但我不想返回一个新地图,我想将result 设置为something.value == someOtherThing.value 是否为真。

但我迷失在函数式 Kotlin 的新手世界中。

谁能把我推向正确的方向?

谢谢

【问题讨论】:

    标签: kotlin functional-programming


    【解决方案1】:

    您可以为此使用any

    val result = somethingList.any { it.value == someOtherThing.value }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-28
      • 2017-12-29
      • 2021-04-15
      • 2012-01-27
      • 2016-08-15
      • 2014-10-26
      • 2018-02-20
      相关资源
      最近更新 更多