【问题标题】:Is there a better way for filter null in a list? [duplicate]有没有更好的方法来过滤列表中的空值? [复制]
【发布时间】:2016-09-06 13:49:22
【问题描述】:

我这样做是为了过滤空值列表:

val myList: List<Any?> = [...]
myList.filter { it != null }.map { it!! }
myList.get(0).xxx // don't need to write "?."

如果我不添加map,列表就不会变成List&lt;...&gt;。有没有更优雅的方法来做到这一点?

【问题讨论】:

  • 你可以使用mylist.filterNotNull()[0].xxx

标签: casting null functional-programming kotlin


【解决方案1】:

像这样使用filterNotNull

val onlyPresent = myList.filterNotNull()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    • 2013-04-04
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    相关资源
    最近更新 更多