3 ms·
True, but the stream API isn't very friendly. You have to get a stream (.stream()), filter, then collect in a collection (.collect(..)) for which you need to fi
by norswap 9y ago
True, but the stream API isn't very friendly. You have to get a stream (.stream()), filter, then collect in a collection (.collect(..)) for which you need to find a suitable collector (the stdlib supplies some for lists and maps, but you have to find where they are defined).
These are small things, and easily corrected with a bit of know-how and/or utility methods. But I think it has had a huge impact on the (non-)adoption of streams.
Another obvious API gap: going from an iterator or iterable to a stream requires much more ceremony than it should.
- vbezhenar 9y agoActually I'm not sure if Kotlin's code is that performant. It'll create new ArrayList on each step just to discard it after. It won't matter for tiny lists, but even for moderate lists it does matter. If you want to iterate without creating new arrays on each step, you'll need to use Sequence. And now there's another problem: Sequence doesn't know its lengths. So when you'll collect computation result, you can't just allocate exact memory, you'll need to allocate some memory, then grow it, and so on. Java streams are lazy and they try hard to know expected output size, so they are better in some aspects. That said, Sequence have very simple implementations while Java Streams are hairy monsters under the hood and you can use Java Streams from Kotlin without problems, if you're targeting JDK8.