是否可以在 Android API 上使用 Java 8 Stream API <24? [英] Is it possible to use the Java 8 Stream API on Android API < 24?
问题描述
我读过这个在此处发布.但是我仍然无法在 minSdkVersion < 上运行包含 Java 8 Stream API 功能的代码,如下所示.24.
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
调用需要 API 级别 24(当前最小值为 15):java.util.Collection#stream
Call requires API level 24 (current min is 15): java.util.Collection#stream
那么有人知道解决方案吗?
So does someone know a solution?
推荐答案
[原答案]
您不能在 API 级别使用 Java8 流 <24.
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/(@sartorius 在评论中提到)
https://sourceforge.net/projects/streamsupport/ (mentioned by @sartorius in comment)
[更新 k3b 2019-05-23]
https://github.com/retrostreams/android-retrostreams 是 streamsupport 的衍生产品它利用了 Android Studio 3.x D8/desugar 工具链的能力来使用界面默认 &跨 Jar 文件边界的静态方法.还有其他 android-retroXXX 的链接,即 CompletableFuture.
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.
[更新aeracode 2020-07-24]
好消息,现在我们可以使用 Java 8 StreamAPI 等不需要最低 API 级别.
Good news, now we can use Java 8 Stream API and more without requiring a minimum API level.
这篇关于是否可以在 Android API 上使用 Java 8 Stream API <24?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!