stream() method does not work in android

2019-01-28 03:47发布

How can I convert this statement from classic java to android

Collection<Integer> p1;   
int sum = p1.stream().mapToInt(Integer::intValue).sum();

I integrate Java 8 and lambda to my android application, but it still does not work. The method stream() does not found in android.

Can you help me?

2条回答
对你真心纯属浪费
2楼-- · 2019-01-28 04:23

You can use the streamsupport library which backports the stream API to Java 6/7 which can be used for Android development, supported on all devices.

Afaik, this library fully backports the original implementation present in Java 8 and makes it available in a separate package (prefix java8.). The added default methods on the Collection interface (e.g. #stream(), #forEach()) are available from separate utility classes, e.g. Iterables or StreamSupport.

查看更多
Root(大扎)
3楼-- · 2019-01-28 04:31

Stream API is included in a recent revision of Android 7.0 SDK Platform (API 24):

public interface Collection<E> extends Iterable<E> {
    // .. redacted
    default Stream<E> stream() { .. }
}

Please note that in order to use it you have to set minSdkVersion to 24.

查看更多
登录 后发表回答