Tag: collections

相当于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 […]

在kotlin中处理流

Kotlin如何运行以下代码? 将5000000个整数的集合创建为临时集合还是将filter将其结果立即送到forEach ,这意味着只有20个整数将被查看? 如果不是,我将如何能够避免中间收集? 码: class Tests { @Test fun test() { var counter = 0 (1..10_000_000).filter { it % 2 == 1 }.forEach { counter++ if (counter > 10) return } } }