February 2018
Intermediate to advanced
350 pages
7h 35m
English
We can also create a Stream by using the Stream.generate() factory method. It accepts a lambda/supplier instance as a parameter, and will use it to generate the item for each time the item is demanded. This method also creates an infinite Stream.
Consider the following example:
fun main(args: Array<String>) {
val stream = Stream.generate {
//return a random number
(1..20).random()
}
val resultantList = stream
.limit(10)
.collect(Collectors.toList())
println("resultantList = $resultantList")
}
The output is as follows:

So, the Stream API called the lambda to get each of the elements of the Stream—awesome. ...
Read now
Unlock full access