美文网首页kotlin
66.集合拆分

66.集合拆分

作者: 写代码的向日葵 | 来源:发表于2019-10-06 01:47 被阅读0次

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)

相关文章

网友评论

    本文标题:66.集合拆分

    本文链接:https://www.haomeiwen.com/subject/diaypctx.html