June 2018
Intermediate to advanced
310 pages
6h 32m
English
Be careful with those functions on large collections, though. Most of them will copy the collection for the sake of immutability.
Functions starting with as won't do that, though:
// Returns a view, no copy here(1..10).toList().asReversed()// Same here(1..10).toList().asSequence()
To understand the difference, check the following code:
val numbers = (1..1_000_000).toList()println(measureTimeMillis { numbers.stream().map { it * it }}) // ~2msprintln(measureTimeMillis { numbers.map { it * it }}) // ~19ms
You'll notice that code using stream() actually never executes. Streams, being lazy, wait for a terminating function call. Functions on collections, on the other hand, execute one after the other.
If we ...
Read now
Unlock full access