Is it possible to use the Java 8 Stream API on Android API < 24?

unlimited101 :

I've read this post here. But still I cannot run code containing Java 8 Stream API features like the following on minSdkVersion < 24.

List<Car> newCars = cars.stream()
                        .filter(s -> s.getColor().equals("red"))
                        .collect(Collectors.toList());

This doesn't run due to the error message

Call requires API level 24 (current min is 15): java.util.Collection#stream

So does someone know a solution?

Robert Estivill :

You can not use Java8 streams on API level < 24.

However, there are some libraries that backport some of the stream functionality

https://github.com/aNNiMON/Lightweight-Stream-API

https://github.com/konmik/solid

https://sourceforge.net/projects/streamsupport/ (mentioned by @sartorius in comment)

[Update k3b 2019-05-23]

https://github.com/retrostreams/android-retrostreams is a spinoff from streamsupport which takes advantage of Android Studio 3.x D8 / desugar toolchain's capability to use interface default & static methods across Jar file boundaries. There are also links to other android-retroXXX ie for CompletableFuture.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=419567&siteId=1