Java 8 Stream Equivalents

suggest change

Introduction

Kotlin provides many extension methods on collections and iterables for applying functional-style operations. A dedicated Sequence type allows for lazy composition of several such operations.

Remarks

About laziness

If you want to lazily process a chain, convert to a Sequence with asSequence() before the chain. At the end of the chain, you usually still have a Sequence. Then you can use toList(), toSet(), toMap(), or some other function to materialize the Sequence.

// switch to and from lazy
val someList = items.asSequence().filter { ... }.take(10).map { ... }.toList()
// stays lazy: Sequence.sorted() returns another Sequence
val someList = items.asSequence().filter { ... }.take(10).map { ... }.sorted()

Why are there no Types?!?

You will notice the Kotlin examples do not specify the types. This is because Kotlin has full type inference and is completely type safe at compile time. More so than Java because it also has nullable types and can help prevent the dreaded NPE. So this in Kotlin:

val someList = people.filter { it.age <= 30 }.map { it.name }

is the same as:

val someList: List<String> = people.filter { it.age <= 30 }.map { it.name }

Because Kotlin knows what people is, and that each element’s age is Int, the filter expression only allows comparison with an Int. Because name is a String, the map step produces a List<String> (a read-only List of String).

Now, if people were possibly null, as in a List<Person>?, then:

val someList = people?.filter { it.age <= 30 }?.map { it.name }

Returns a List<String>? that would need to be null checked (or use one of the other Kotlin operators for nullable values, see this Kotlin idiomatic way to deal with nullable values and also Idiomatic way of handling nullable or empty list in Kotlin)

Reusing Streams

In Kotlin, it depends on the type of collection whether it can be consumed more than once. A Sequence generates a new iterator every time, and unless it asserts “use only once” it can reset to the start each time it is acted upon. Therefore the following fails for a Java 8 stream, but works in Kotlin:

// Java:
Stream<String> stream =
Stream.of("d2", "a2", "b1", "b3", "c").filter(s -> s.startsWith("b"));

stream.anyMatch(s -> true);    // ok
stream.noneMatch(s -> true);   // exception
// Kotlin:  
val stream = listOf("d2", "a2", "b1", "b3", "c").asSequence().filter { it.startsWith('b' ) }

stream.forEach(::println) // b1, b3

println("Any B ${stream.any { it.startsWith('b') }}") // Any B true
println("Any C ${stream.any { it.startsWith('c') }}") // Any C false

stream.forEach(::println) // b1, b3

And in Java to get the same behavior:

// Java:
Supplier<Stream<String>> streamSupplier =
    () -> Stream.of("d2", "a2", "b1", "b3", "c")
          .filter(s -> s.startsWith("b"));

streamSupplier.get().anyMatch(s -> true);   // ok
streamSupplier.get().noneMatch(s -> true);  // ok

Therefore in Kotlin the provider of the data decides if it can reset back and provide a new iterator or not. But if you want to intentionally constrain a Sequence to one time iteration, you can use constrainOnce() function for Sequence as follows:

val stream = listOf("d2", "a2", "b1", "b3", "c").asSequence().filter { it.startsWith('b' ) }
        .constrainOnce()

stream.forEach(::println) // b1, b3
stream.forEach(::println) // Error: java.lang.IllegalStateException: This sequence can be consumed only once.

See also:

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents