如何将迭代器转换为流?

我正在寻找一种简洁的方法将Iterator转换为Stream或更具体地“查看”迭代器作为一个流。

出于性能考虑,我希望避免在新列表中复制迭代器:

Iterator<String> sourceIterator = Arrays.asList("A", "B", "C").iterator();
Collection<String> copyList = new ArrayList<String>();
sourceIterator.forEachRemaining(copyList::add);
Stream<String> targetStream = copyList.stream();

根据评论中的一些建议,我也尝试使用Stream.generate:

public static void main(String[] args) throws Exception {
Iterator<String> sourceIterator = Arrays.asList("A", "B", "C").iterator();
Stream<String> targetStream = Stream.generate(sourceIterator::next);
targetStream.forEach(System.out::println);
}

然而,我得到了一个NoSuchElementException(因为没有调用hasNext)

Exception in thread "main" java.util.NoSuchElementException
at java.util.AbstractList$Itr.next(AbstractList.java:364)
at Main$$Lambda$1/1175962212.get(Unknown Source)
at java.util.stream.StreamSpliterators$InfiniteSupplyingSpliterator$OfRef.tryAdvance(StreamSpliterators.java:1351)
at java.util.Spliterator.forEachRemaining(Spliterator.java:326)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
at Main.main(Main.java:20)

我已经看了StreamSupportCollections,但我没有发现任何东西。

301301 次浏览

一种方法是从Iterator创建一个Spliterator,并使用它作为你的流的基础:

Iterator<String> sourceIterator = Arrays.asList("A", "B", "C").iterator();
Stream<String> targetStream = StreamSupport.stream(
Spliterators.spliteratorUnknownSize(sourceIterator, Spliterator.ORDERED),
false);

另一种可能更具可读性的方法是使用Iterable,并且使用lambdas从Iterator创建Iterable非常容易,因为Iterable是一个函数接口:

Iterator<String> sourceIterator = Arrays.asList("A", "B", "C").iterator();


Iterable<String> iterable = () -> sourceIterator;
Stream<String> targetStream = StreamSupport.stream(iterable.spliterator(), false);
< p >好建议! 下面是我对它的可重用性:

public class StreamUtils {


public static <T> Stream<T> asStream(Iterator<T> sourceIterator) {
return asStream(sourceIterator, false);
}


public static <T> Stream<T> asStream(Iterator<T> sourceIterator, boolean parallel) {
Iterable<T> iterable = () -> sourceIterator;
return StreamSupport.stream(iterable.spliterator(), parallel);
}
}

和用法(确保静态导入asStream):

List<String> aPrefixedStrings = asStream(sourceIterator)
.filter(t -> t.startsWith("A"))
.collect(toList());

Iterator创建Spliterator使用Spliterators类包含多个函数来创建spliterator,例如这里我使用spliteratorUnknownSize,它是获取迭代器作为参数,然后使用StreamSupport创建流

Spliterator<Model> spliterator = Spliterators.spliteratorUnknownSize(
iterator, Spliterator.NONNULL);
Stream<Model> stream = StreamSupport.stream(spliterator, false);

从版本21开始,Guava库提供了Streams.stream(iterator)

它做什么@assylias答案显示

使用# EYZ0

这在Java 9中是可能的。

Stream.generate(() -> null)
.takeWhile(x -> iterator.hasNext())
.map(n -> iterator.next())
.forEach(System.out::println);
import com.google.common.collect.Streams;

然后使用Streams.stream(iterator):

Streams.stream(iterator)
.map(v-> function(v))
.collect(Collectors.toList());

在Java 9+上使用Stream::iterate(T, Predicate, UnaryOperator)的另一种方法:

Stream.iterate(iterator, Iterator::hasNext, UnaryOperator.identity())
.map(Iterator::next)
.forEach(System.out::println);

1 assylias的解决方案包装在一个方法:

public static <T> Stream<T> toStream(Iterator<T> iterator) {
return StreamSupport.stream(((Iterable<T>)() -> iterator).spliterator(), false);
}

2 番石榴流实现(标记为@Beta):

public static <T> Stream<T> stream(Iterator<T> iterator) {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0), false);
}