[英]How to create a Java 8 Stream from an iterator?
Is it possible to create a Stream from an Iterator, in which the sequence of objects is the same as that generated by calling the iterator's next() method repeatedly?是否可以从迭代器创建一个流,其中对象的序列与通过重复调用迭代器的 next() 方法生成的序列相同? The specific case I am thinking of concerns the use of the iterator returned by TreeSet.descendingIterator(), but I can imagine other circumstances in which an iterator, but not the collection it references, is available.
我正在考虑的具体情况与 TreeSet.descendingIterator() 返回的迭代器的使用有关,但我可以想象在其他情况下迭代器可用,而不是它引用的集合。
For example, for a TreeSet<T> tset
we can write tset.stream()...
and get a stream of the objects in that set, in the set's sort order, but what if we want them in a different order, such as that available through using descendingIterator()
?例如,对于
TreeSet<T> tset
我们可以编写tset.stream()...
并按照集合的排序顺序获取该集合中对象的流,但是如果我们希望它们以不同的顺序排列,例如可以通过使用descendingIterator()
吗? I am imagining something like tset.descendingIterator().stream()...
or stream( tset.descendingIterator() )...
, though neither of these forms are valid.我正在想象像
tset.descendingIterator().stream()...
或stream( tset.descendingIterator() )...
,尽管这两种形式都无效。
static <T> Stream<T> iteratorToFiniteStream(final Iterator<T> iterator) {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0), false);
}
static <T> Stream<T> iteratorToInfiniteStream(final Iterator<T> iterator) {
return Stream.generate(iterator::next);
}
For the particular example of NavigableSet.descendingIterator()
, I think the simplest way is to use NavigableSet.descendingSet()
instead.对于
NavigableSet.descendingIterator()
的特定示例,我认为最简单的方法是使用NavigableSet.descendingSet()
代替。
But given you are probably interested in the more general case, the following seems to work:但鉴于您可能对更一般的情况感兴趣,以下似乎有效:
import java.util.Iterator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.TreeSet;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class Streams {
public static void main(String... args) {
TreeSet<String> set = new TreeSet<>();
set.add("C");
set.add("A");
set.add("B");
Iterator<String> iterator = set.descendingIterator();
int characteristics = Spliterator.DISTINCT | Spliterator.SORTED | Spliterator.ORDERED;
Spliterator<String> spliterator = Spliterators.spliteratorUnknownSize(iterator, characteristics);
boolean parallel = false;
Stream<String> stream = StreamSupport.stream(spliterator, parallel);
stream.forEach(System.out::println); // prints C, then B, then A
}
}
In short, you have to create a Spliterator
from the Iterator
first using one of the static methods in Spliterators
.总之,你必须创建一个
Spliterator
从Iterator
首先使用的静态方法之一Spliterators
。 Then you can create a Stream
using the static methods in StreamSupport
.然后您可以使用
StreamSupport
的静态方法创建一个Stream
。
I don't have that much experience with creating Spliterators and Streams by hand yet, so I can't really comment on what the characteristics should be or what effect they will have.我还没有多少手动创建 Spliterators 和 Streams 的经验,所以我无法真正评论应该具有什么特征或它们将产生什么效果。 In this particular simple example, it didn't seem to matter whether I defined the characteristics as above, or whether I set it to 0 (ie no characteristics).
在这个特别简单的例子中,我是否如上定义特征,或者是否将其设置为 0(即没有特征)似乎并不重要。 There is also a method in
Spliterators
for creating a Spliterator with an initial size estimate - I suppose in this particular example you could use set.size()
, but if you want to handle arbitrary Iterators I guess this won't be the case. Spliterators
还有一种方法可以创建具有初始大小估计的 Spliterator - 我想在这个特定示例中您可以使用set.size()
,但是如果您想处理任意迭代器,我想情况并非如此。 Again, I'm not quite sure what effect it has on performance.同样,我不太确定它对性能有什么影响。
This doesn't create a stream, but Iterator
also has a method called forEachRemaining
:这不会创建流,但
Iterator
也有一个名为forEachRemaining
的方法:
someIterator.forEachRemaining(System.out::println);
someIterator.forEachRemaining(s -> s.doSomething());
//etc.
The argument you pass in is a Consumer which is the same thing you pass to Stream::forEach .您传入的参数是一个Consumer ,它与您传递给Stream::forEach 的内容相同。
Here are the docs for that method. 这是该方法的文档。 note that you can't continue the "chain" like you can with a stream.
请注意,您不能像使用流一样继续“链” 。 But I've still found this helpful the few times I've wanted a Stream from an Iterator.
但是我几次想要来自迭代器的 Stream 时,我仍然发现这很有帮助。
As it was written by Karol Król for infinite stream you can use this:正如Karol Król为无限流编写的那样,您可以使用它:
Stream.generate(iterator::next)
but you can also use it for finite stream with takeWhile
since Java 9但你也可以将它用于有限流和
takeWhile
自 Java 9
Stream.generate(iterator::next).takeWhile((v) -> iterator.hasNext())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.