Kotlin Sequences vs Java Streams
SebaMutuku
Posted on March 18, 2023
If you don't have any clue about Kotlin sequences, you can refer to this post.
Sequences are similar to java streams. In fact when they were originally introduced in kotlin
, they were called streams. We can dwell much in java streams
.
The choice of when to use java streams
or kotlin
sequences depends on the platform
- Java streams were introduced in Java 8, so if you’re running on a version of Java older than that, you need to use
kotlin
sequences instead. - On
Android
,Java
stream support began in Android 7.0, so if you’re targeting a minimum version of the OS below that,sequences
are your option. - If you’re developing a
kotlin
library, it’d be wise to stick with sequences and other purekotlin
concepts, because it enables your library to target multiple platforms.
Now having know the above let us dive into sample code snippets
Java Streams
Let us pick the computations below for example
List finalList = List.of(
List.of("myFirstName", "mySecondName", "myThirdName", "myOtherName", "", ""),
List.of("abFirstName", "abFirstName", "abThirdName", "abOtherName", "", ""));
List returnList = Collections.singletonList(finalList.stream()
.filter(item -> !item.toString().isEmpty())
.flatMap(item -> item.toString())
.sorted().map(itemList -> itemList.toString().toUpperCase()));
The above sample code snippet to demonstrates several operations in java streams which are sequential in nature. However, since we have multiprocessing, java changed the game by introduction of parallel streams
.
Java Parallel Streams
Java streams huge advantage over Kotlin sequences is that streams can be run in parallel
. Sequences
do not run in parallel - after all, they are sequential by definition!
Running your operation chains in parallel comes with a startup cost, but once you overcome that cost, it’s generally hard to beat their performance in many cases.
Parallel streams
enable us to execute code in parallel on separate cores. The final result is the combination of each individual outcome.So if you’re on the JVM and you’re processing lots of data with an operation chain that’s amenable to running in parallel, it’d be worth considering Java’s parallel streams.
Assume the example below to demonstrate it. To run java streams in parallel, you just need to add parallel()
or using parallelStream()
on a Collection before performing list of operations
Example 1
IntStream rangeofInts = IntStream.rangeClosed(1, 10);
rangeofInts.parallel().forEach(System.out::println);
Example 2
List returnList = Collections.singletonList(finalList.parallelStream()
.filter(item -> !item.toString().isEmpty())
.flatMap(item -> item.toString())
.sorted().map(itemList -> itemList.toString().toUpperCase()));
Performance Implications
Parallelism can bring performance benefits in certain use cases. But parallel streams
cannot be considered as a magical performance booster. So, sequential streams should still be used as default during development.
In conclusion,although sequences can perform much better than collections, there are still plenty of cases where collections get the edge.
Posted on March 18, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024