如何从迭代器创建 Java 8 Stream?

How to create a Java 8 Stream from an iterator?(如何从迭代器创建 Java 8 Stream?)

本文介绍了如何从迭代器创建 Java 8 Stream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从 Iterator 创建 Stream,其中对象的序列与重复调用迭代器的 next() 方法生成的对象序列相同?我正在考虑的具体情况涉及 TreeSet.descendingIterator() 返回的迭代器的使用,但我可以想象在其他情况下迭代器可用,但它引用的集合不可用.

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? 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<T>tset 我们可以编写 tset.stream()... 并按照集合的排序顺序获取该集合中的对象流,但是如果我们想要它们以不同的顺序怎么办,例如通过使用 descendingIterator() 可获得的?我在想像 tset.descendingIterator().stream()...stream( tset.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()? I am imagining something like tset.descendingIterator().stream()... or stream( tset.descendingIterator() )..., though neither of these forms are valid.

推荐答案

对于NavigableSet.descendingIterator()的具体例子,我认为最简单的方法是使用NavigableSet.descendingSet()而是.

For the particular example of NavigableSet.descendingIterator(), I think the simplest way is to use NavigableSet.descendingSet() instead.

但鉴于您可能对更一般的情况感兴趣,以下似乎可行:

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
    }
}

简而言之,您必须创建一个 Spliterator 来自 Iterator 首先使用 Spliterators.然后您可以使用 Stream" rel="noreferrer">StreamSupport.

In short, you have to create a Spliterator from the Iterator first using one of the static methods in Spliterators. Then you can create a Stream using the static methods in StreamSupport.

我还没有太多手动创建 Spliterators 和 Streams 的经验,所以我无法真正评论这些特征应该是什么或它们会产生什么影响.在这个特别简单的例子中,我是否定义了上述特征,或者是否将其设置为 0(即没有特征)似乎并不重要.Spliterators 中还有一种方法可以创建具有初始大小估计的 Spliterator - 我想在这个特定示例中您可以使用 set.size(),但如果您愿意处理任意迭代器我想这不会是这种情况.同样,我不太确定它对性能有什么影响.

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. 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 (i.e. no characteristics). 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. Again, I'm not quite sure what effect it has on performance.

这篇关于如何从迭代器创建 Java 8 Stream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何从迭代器创建 Java 8 Stream?

基础教程推荐