Kotlin等效于Groovy spead点(*)

Kotlin等效于Groovy spead点(*),groovy,collections,kotlin,Groovy,Collections,Kotlin,根据以下代码,要在一个漂亮的列表中获得所有帐号的列表,我必须执行以下操作: data class Customer(val accounts : List<Account>) data class Account(val number : String) fun getCustomers() = arrayListOf( Customer( arrayListOf(Account("1"),Account("2")) ), Customer(

根据以下代码,要在一个漂亮的列表中获得所有帐号的列表,我必须执行以下操作:

data class Customer(val accounts : List<Account>)
data class Account(val number : String)

fun getCustomers() = arrayListOf(
    Customer(
        arrayListOf(Account("1"),Account("2"))
    ),
    Customer(
        arrayListOf(Account("3"),Account("4"))
    )
)
fun main(args: Array<String>) {
    // long
    println(getCustomers().map{ it.accounts }.flatten().map{ it.number })
    // a little shorter (just discovered while typing the question)
    println(getCustomers().flatMap{ it.accounts }.map{ it.number })


哪一个稍微好一点。是否可以“创建”一个操作符(比如*)来执行与Groovy版本类似的操作

不,不可能在Kotlin中创建新的运算符

    println(getCustomers()*.accounts*.number.flatten())
    // or even
    println(getCustomers().accounts.number.flatten())