如何在Iterable上执行Stream功能? [重复]

时间:2021-07-09 21:44:10

This question already has an answer here:

这个问题在这里已有答案:

In Java 8, the Stream class does not have any method to wrap a an Iterable.

在Java 8中,Stream类没有任何方法来包装Iterable。

Instead, I am obtaining the Spliterator from the Iterable and then obtaining a Stream from StreamSupport like this:

相反,我从Iterable获取Spliterator,然后从StreamSupport获取Stream,如下所示:

boolean parallel = true;

StreamSupport.stream(spliterator(), parallel)
                .filter(Row::isEmpty)
                .collect(Collectors.toList())
                .forEach(this::deleteRow);

Is there some other way of generating Stream operations on an Iterable that I am missing?

是否有其他方法可以在Iterable上生成Stream操作,而我却错过了?

3 个解决方案

#1


36  

My similar question got marked as duplicate, but here is the helper methods I've used to avoid some of the boilerplate:

我的类似问题被标记为重复,但这里是我用来避免一些样板的辅助方法:

public static <T> Stream<T> stream(Iterable<T> in) {
    return StreamSupport.stream(in.spliterator(), false);
}

public static <T> Stream<T> parallelStream(Iterable<T> in) {
    return StreamSupport.stream(in.spliterator(), true);
}

#2


1  

What you describe is the way to get a stream from an Iterable. That's why they added the spliterator() method to Iterable. I've done the same conversion myself and have not seen another way.

你描述的是从Iterable获取流的方法。这就是他们将spliterator()方法添加到Iterable的原因。我自己完成了相同的转换,并没有看到另一种方式。

[UPDATE] Maybe this other answer will shed some clarification on the "why."

[更新]也许这个其他答案会对“为什么”做出一些澄清。

#3


1  

I know this doesn't directly answer your question, but a decent number of Iterable sources such as collections now have a method to get the object as a stream as well.

我知道这并没有直接回答你的问题,但是大量的Iterable资源(例如集合)现在都有一种方法可以将对象作为流来获取。

I think that the friction that you will run into with this question is that Iterable is semantically serial whereas Spliterators are meant to be used for processing in parallel. It is probably a better idea to implement a Spliterator for the underlying data source that you are interested in if it is not already provided in the JDK because just using a wrapper around the Iterable will not allow you to gain the benefits that the Stream API provide (such as parallel processing).

我认为你将遇到这个问题的摩擦是Iterable在语义上是串行的,而Spliterators是用来并行处理的。如果JDK中尚未提供Spliterator,那么为您感兴趣的基础数据源实现Spliterator可能更好一点,因为仅使用Iterable周围的包装器将无法获得Stream API提供的好处(如并行处理)。

#1


36  

My similar question got marked as duplicate, but here is the helper methods I've used to avoid some of the boilerplate:

我的类似问题被标记为重复,但这里是我用来避免一些样板的辅助方法:

public static <T> Stream<T> stream(Iterable<T> in) {
    return StreamSupport.stream(in.spliterator(), false);
}

public static <T> Stream<T> parallelStream(Iterable<T> in) {
    return StreamSupport.stream(in.spliterator(), true);
}

#2


1  

What you describe is the way to get a stream from an Iterable. That's why they added the spliterator() method to Iterable. I've done the same conversion myself and have not seen another way.

你描述的是从Iterable获取流的方法。这就是他们将spliterator()方法添加到Iterable的原因。我自己完成了相同的转换,并没有看到另一种方式。

[UPDATE] Maybe this other answer will shed some clarification on the "why."

[更新]也许这个其他答案会对“为什么”做出一些澄清。

#3


1  

I know this doesn't directly answer your question, but a decent number of Iterable sources such as collections now have a method to get the object as a stream as well.

我知道这并没有直接回答你的问题,但是大量的Iterable资源(例如集合)现在都有一种方法可以将对象作为流来获取。

I think that the friction that you will run into with this question is that Iterable is semantically serial whereas Spliterators are meant to be used for processing in parallel. It is probably a better idea to implement a Spliterator for the underlying data source that you are interested in if it is not already provided in the JDK because just using a wrapper around the Iterable will not allow you to gain the benefits that the Stream API provide (such as parallel processing).

我认为你将遇到这个问题的摩擦是Iterable在语义上是串行的,而Spliterators是用来并行处理的。如果JDK中尚未提供Spliterator,那么为您感兴趣的基础数据源实现Spliterator可能更好一点,因为仅使用Iterable周围的包装器将无法获得Stream API提供的好处(如并行处理)。