Java 8 Streams are powerful but when parallelism is not needed Kotlin Sequence seems to be simpler to use.
Is there a way to convert a stream.sequencial() into a Sequence?
Java 8 Streams are powerful but when parallelism is not needed Kotlin Sequence seems to be simpler to use.
Is there a way to convert a stream.sequencial() into a Sequence?
You can get an iterator from a stream and then wrap the iterator into a Sequence:
Sequence { stream.iterator() }
UPD: Starting from Kotlin 1.1 you can use Stream.asSequence() extension (see Michael Richardson's answer), which does the exact same thing as above. The extension is also available for the specialized streams: IntStream, LongStream and DoubleStream.
It is located in kotlin.streams package in kotlin-stdlib-jdk8 library (Kotlin 1.2 and above) or in kotlin-stdlib-jre8 library (Kotlin 1.1 only, deprecated in Kotlin 1.2 and above).
Kotlin has an extension method asSequence() to convert a Java stream to a Kotlin Sequence. In my experience it was not discoverable until I added an import statement:
import kotlin.streams.*
Then simply use as expected:
val seq = stream.asSequence()