- How will Kotlin work the following code?
- Will a collection of 5000000 integers be created as temporary collection or will the filter feed its result immediately to the
forEach
which means, that only 20 integers will be looked at? - If not, how would I be able to avoid the intermediate collection?
Code:
class Tests {
@Test
fun test() {
var counter = 0
(1..10_000_000).filter { it % 2 == 1 }.forEach {
counter++
if (counter > 10)
return
}
}
}