是否可以在 Android API < 24 上使用 Java 8 Stream API?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39515035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to use the Java 8 Stream API on Android API < 24?
提问by unlimited101
I've read this posthere. But still I cannot run code containing Java 8 Stream API features like the following on minSdkVersion < 24.
我在这里读过这篇文章。但是我仍然无法在 minSdkVersion < 24 上运行包含 Java 8 Stream API 功能的代码,如下所示。
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
调用需要 API 级别 24(当前最小值为 15):java.util.Collection#stream
So does someone know a solution?
那么有人知道解决方案吗?
采纳答案by Robert Estivill
You can not use Java8 streams on API level < 24.
您不能在 API 级别 < 24 上使用 Java8 流。
However, there are some libraries that backport some of the stream functionality
但是,有一些库可以向后移植一些流功能
https://github.com/aNNiMON/Lightweight-Stream-API
https://github.com/aNNiMON/Lightweight-Stream-API
https://github.com/konmik/solid
https://github.com/konmik/solid
https://sourceforge.net/projects/streamsupport/(mentioned by @sartorius in comment)
https://sourceforge.net/projects/streamsupport/(@sartorius在评论中提到)
[Update k3b 2019-05-23]
[更新k3b 2019-05-23]
https://github.com/retrostreams/android-retrostreamsis 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.
https://github.com/retrostreams/android-retrostreams是 streamsupport 的衍生产品,它利用了 Android Studio 3.x D8 / desugar 工具链在 Jar 文件边界上使用接口默认和静态方法的能力。还有其他 android-retroXXX 的链接,即 CompletableFuture。
回答by T. Neidhart
Since release 8.2 of DexGuardit is possible to use the Java 8 streams API also on Android devices < API level 24. In order to do so, one needs to include the streamsupportlibrary and DexGuard will translate all Java 8 stream API calls to the provided library. No additional handling is needed, and developers can simply code using the provided Java 8 streams API. Dependencies are also translated automatically, so it is possible to use libraries with Java 8 features also for Android development.
从DexGuard8.2 版开始,也可以在低于 API 级别 24 的 Android 设备上使用 Java 8 流 API。为此,需要包含streamsupport库,DexGuard 会将所有 Java 8 流 API 调用转换为提供的图书馆。不需要额外的处理,开发人员可以简单地使用提供的 Java 8 流 API 进行编码。依赖项也会自动转换,因此可以将具有 Java 8 特性的库也用于 Android 开发。
This feature will also be included in ProGuard in the near future, stay tuned.
此功能也将在不久的将来包含在 ProGuard 中,敬请期待。
Edit: Proguard 6.1.0 for which there already exists a beta version supports backporting Java 8 stream and time API.
编辑:Proguard 6.1.0 已经存在一个测试版,支持向后移植 Java 8 流和时间 API。
回答by Yusuf Ayd?n
Create a ?nterface.
创建一个界面。
public interface Pre<T,R> {
R get(T item);
}
Create a Filter Method.
创建过滤器方法。
public static <T> List<T> Filter(List<T> list, Pre<T,Boolean> pre) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
{
return list.stream().filter(p -> pre.get(p)).collect(Collectors.toList());
}
else
{
List<T> col = new ArrayList<T>();
for (int i = 0 ; i < list.size() ; i++)
if (pre.get(list.get(i)))
col.add(list.get(i));
return col;
}
}
Using Code
使用代码
public static class model {
public String Name;
}
List<model> models = new ArrayList<>();
...
List<model> filtermodels = Filter(models,p-> p.Name.equals("filter"));
回答by Abhriya Roy
Might be a little late to the party, but I just saw that If we use java8.util.stream.StreamSupport
then we can run the same functional programming approach and use it prior to Api 24. Eg taken from web3j -
参加聚会可能有点晚了,但我刚刚看到如果我们使用,java8.util.stream.StreamSupport
那么我们可以运行相同的函数式编程方法并在 Api 24 之前使用它。例如,取自 web3j -
StreamSupport.stream(transactionReceipt.getLogs())
.map(log -> extractEventParameters(event, log))
.filter(Objects::nonNull)
.collect(Collectors.toList());
回答by AjahnCharles
Since 2017 => Consider Using Kotlin
自 2017 年起 => 考虑使用 Kotlin
This question was originally posted in 2016, when Java was the only choice. Today, Kotlin is the first-language of Android. For new readers, this is likely a preferable choice (no API restrictions and much shorter):
这个问题最初是在 2016 年发布的,当时 Java 是唯一的选择。今天,Kotlin 是 Android 的第一语言。对于新读者来说,这可能是一个更好的选择(没有 API 限制,而且更短):
val newCars = cars.filter { it.getColor() == "red" }
See Abhriya Roy's StreamSupport answerfor a Java-only solution
有关纯Java 解决方案,请参阅Abhriya Roy 的 StreamSupport 答案