<@U68N4SR27> Don't call `.stream()` it gives you a...
# announcements
h
@ianandrewclark Don't call
.stream()
it gives you a java stream not a kotlin list, you should be able to call
.forEach { println(it) }
directly on the collection.
s
Am I missing context? Why would you suggest mapping the stream contents with
println
? Might he not need the list for something else?
Also it might be more concise to just
.forEach(::println)
h
My apologies, I made an assumption he was try to convert something to a list in order to perform some sort of list functions on it. map/filter/foreach. Furthermore if he can call stream on the object then isn't it most likely already a collection?
Any yes that is more concise 😉
i
Sorry, I’m getting the Stream from a BufferedReader.lines() method
Is there a convenient way I can get a Sequence instead?
s
hmm, there’s supposed to be an extension function,
BufferedReader#lineSequence()
, provided by Kotlin, but I’m not sure that’ll be terribly helpful if you can’t use any of the extension methods
👍 1
i
Hmm, let me check that out…
Nice, that worked, thank you @Shawn;)
👍 1
s
np @Ian. Might be worth mentioning that the source for that method includes this comment:
Copy code
We suggest you try the method [useLines] instead which closes the stream when the processing is complete.
I’m not sure if this is directly relevant to you, but it may be worth investigating
useLines
takes in a lambda, specifically
(Sequence<String>) -> T
, and handles cleanup for you
i
Yes, in this case I actually needed a
List
, so toList() worked out fine 🙂
👍 1