相当于Groovy的Kotlin spead-dot(*。)

鉴于下面的代码,以获得一个不错的列表中的所有帐户列表我必须执行以下操作:

data class Customer(val accounts : List) 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) { // 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中,我可以这样做:

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

游乐场链接

哪一个更好一点。 是否有可能“创建”一个运算符(比如*)来做类似于Groovy的版本?

不,不可能在Kotlin创建新的运营商。

    Interesting Posts