Is it possible to send the result of a steam operation to another function without collecting it in another variable

Prasad :

For example, in Kotlin/java I have a list of Strings. I have filtered the list and passed it through a mapping function. In the end I can collect it into another list and then use that list anywhere I want like send it to another function. Is there any way to send the output list to another function without explicitly collecting it? Code snippet below:

val resultantList = stringList.filter { it.startsWith("a") }
            .map { it.substringAfter("b") }
anotherFunction(resultantList)

In the above piece of code I dont want to declare resultantList. Instead, at the end of map, can I pass the result to 'anotherFunction` directly. Something like

stringList.filter { it.startsWith("a") }
            .map { it.substringAfter("b") }.<something>{anotherFunction(it)}
Maroš Šeleng :

let is what you are looking for. You don't even have to call .let { anotherFunction(it) } but can use a function reference .let(::anotherFunction)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=150046&siteId=1