第50条:限制操作步骤的数量
class Student(val name: String?)
// 使其工作的代码
fun List<Student>.getNames(): List<String> = this
.map { it.name }
.filter { it != null }
.map { it!! }
// 更好的做法
fun List<Student>.getNames(): List<String> = this
.map { it.name }
.filterNotNull()
// 最好的做法
fun List<Student>.getNames(): List<String> = this
.mapNotNull { it.name }

总结
Last updated