0
我这样做,对空值的过滤列表:有没有更好的方法在列表中过滤null?
val myList: List<Any?> = [...]
myList.filter { it != null }.map { it!! }
myList.get(0).xxx // don't need to write "?."
如果我不添加map
,列表不会成为List<...>
。有没有更好的方法来做到这一点?
我这样做,对空值的过滤列表:有没有更好的方法在列表中过滤null?
val myList: List<Any?> = [...]
myList.filter { it != null }.map { it!! }
myList.get(0).xxx // don't need to write "?."
如果我不添加map
,列表不会成为List<...>
。有没有更好的方法来做到这一点?
使用filterNotNull
像这样:
val onlyPresent = myList.filterNotNull()
你可以用'mylist.filterNotNull()[0] .xxx' –