Chapter 13. Streams to Iterables to Sequences
Java and Kotlin both allow us to transform and reduce collections. They have different design goals and implementations, though. What does Kotlin use instead of Java streams, when should we convert, and how?
Java Streams
Java 8 introduced streams in 2014, making good use of the new lambdas. Say we want to work out the average length of some strings, except that blank strings (those with only whitespace characters) should be treated as if they are empty. Previously we might have written:
public
static
double
averageNonBlankLength
(
List
<
String
>
strings
)
{
var
sum
=
0
;
for
(
var
s
:
strings
)
{
if
(!
s
.
isBlank
())
sum
+=
s
.
length
();
}
return
sum
/
(
double
)
strings
.
size
();
}
With Java streams, we can express this algorithm as filter
, map
, and reduce
by first converting the List
to a Stream
and applying transformations:
public
static
double
averageNonBlankLength
(
List
<
String
>
strings
)
{
return
strings
.
stream
()
.
filter
(
s
->
!
s
.
isBlank
())
.
mapToInt
(
String:
:
length
)
.
sum
()
/
(
double
)
strings
.
size
();
}
Rather than having to run the for-loop in our heads to see what this code is doing, we can see the steps of the algorithm declared line by line and rely on the runtime to implement those steps for us.
If we are really in a hurry for those results, we can even write:
public
static
double
averageNonBlankLength
(
List
<
String
>
strings
)
{
return
strings
.
parallelStream
(
)
.
filter
(
s
-
>
!
s
.
isBlank
(
)
)
.
mapToInt
(
String:
:
length
)
.
sum
(
)
/
(
double
)
strings ...
Get Java to Kotlin now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.