1.用法如下:
/**
* 嵌套匿名函数
*/
fun main(args: Array<String>) {
val list1 = listOf<String>("张三", "李四", "王五", "找六", "张四", "李五", "李六")
val partition = list1.partition { it.startsWith("张") }
println(partition.first)
println(partition.second)
}
2.集合重新组合
fun main(args: Array<String>) {
val persons = listOf<Person>(
Person("林青霞", 50), Person("张曼玉", 30), Person("柳岩", 70), Person("张三", 90),
Person("张三", 20)
)
val list = persons.map { it.name }
println(list)
}
data class Person(val name: String, val age: Int)
3.集合相加
fun main(args: Array<String>) {
val persons = listOf<Person>(
Person("林青霞", 50), Person("张曼玉", 30), Person("柳岩", 70), Person("张三", 90),
Person("张三", 20)
)
val sumBy = persons.sumBy { it.age }
println(sumBy)
val sumByDouble = persons.sumByDouble { it.age.toDouble() }
println(sumByDouble)
}
data class Person(val name: String, val age: Int)
网友评论