https://kotlinlang.org logo
#getting-started
Title
# getting-started
t

thhh

04/27/2021, 4:51 PM
I heard someone say the reason why Kotlin dont use Java 8 Streams is due to Android not supporting Streams, that;s why Kotlin uses sequences and not Streams. Q: Why Android cannot use Java 8 Stream and what's the relation between Streams and Kotlin.
c

Casey Brooks

04/27/2021, 5:13 PM
Android didn’t natively support Java 8 APIs like
Stream
until API 24. You could compile an app against higher SDK versions and use those APIs on devices running Android 7.0 or higher, but on older Android phones those APIs did not exist and would crash. However, now with desugaring, this is not a problem, and you can use many of the Java 8 APIs on older devices (notably LocalDate, Stream, and Optional). This fact has nothing to do with Kotlin at all. However, in the Kotlin world, there’s really no need for Streams. The stdlib contains the same operations available on
Stream
directly on
Collections
without that intermediate Stream layer. Much of what was done with
Stream
was in fact just processing collections with a functional API (using
.map
,
.filter
, etc). In addition, the API of
Stream
is designed to be used from Java, which does not have extension functions, and so is a bit cumbersome to use from Kotlin. For the less-common use-case of
Streams
, where you are actually generating and producing a lazy (potentially infinite) stream of data, Kotlin
Sequences
are basically a complete replacement. They are implemented very differently and are not directly compatible with one another, but you can easily convert a
Stream
into a
Sequence
to make it easier to consume in Kotlin, with
stream.asSequence()
👍 1