一起迭代两个 Java-8-Stream

Iterate two Java-8-Streams together(一起迭代两个 Java-8-Stream)

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

问题描述

我想一起迭代两个 Java-8-Stream,以便在每个迭代步骤中都有两个参数.类似的东西,其中 somefunction 产生类似 Stream<Pair<A,B>> 的东西.

I'd like to iterate two Java-8-Streams together, so that I have in each iteration-step two arguments. Something like that, where somefunction produces something like Stream<Pair<A,B>>.

Stream<A> as;
Stream<B> bs;
somefunction (as, bs)
  .forEach ((a, b) -> foo (a, b));
// or something like
somefunction (as, bs)
  .forEach ((Pair<A, B> abs) -> foo (abs.left (), abs.right ()));

我想知道,如果 Java 提供类似的东西,尽管 Java 中没有 Pair :-(如果没有这样的 API-Function,是否有另一种同时迭代两个流的方法?

I want to know, if Java provides something like that, although there is no Pair in Java :-( If there is no API-Function like that, is there another way of iterating two streams simultaniously?

推荐答案

static <A, B> Stream<Pair<A, B>> zip(Stream<A> as, Stream<B> bs)
{
    Iterator<A> i=as.iterator();
    return bs.filter(x->i.hasNext()).map(b->new Pair<>(i.next(), b));
}

这不提供并行执行,但原始 zip 实现也不提供.

This does not offer parallel execution but neither did the original zip implementation.

作为 F.Böller 已经指出 如果 bs 是无限的而 as 不是,它就不起作用.对于适用于无限和有限流的所有可能组合的解决方案,在 hasNext 方法中检查两个源的中间 Iterator 似乎是不可避免的¹:

And as F. Böller has pointed out it doesn’t work if bs is infinite and as is not. For a solution which works for all possible combinations of infinite and finite streams, an intermediate Iterator which checks both sources within the hasNext method seems unavoidable¹:

static <A, B> Stream<Pair<A,B>> zip(Stream<A> as, Stream<B> bs) {
    Iterator<A> i1 = as.iterator();
    Iterator<B> i2 = bs.iterator();
    Iterable<Pair<A,B>> i=()->new Iterator<Pair<A,B>>() {
        public boolean hasNext() {
            return i1.hasNext() && i2.hasNext();
        }
        public Pair<A,B> next() {
            return new Pair<A,B>(i1.next(), i2.next());
        }
    };
    return StreamSupport.stream(i.spliterator(), false);
}

如果你想要并行压缩,你应该考虑 Streamsource.例如.您可以压缩两个 ArrayList(或任何 RandomAccessList),例如

If you want parallel capable zipping you should consider the source of the Stream. E.g. you can zip two ArrayLists (or any RandomAccessList) like

ArrayList<Foo> l1=new ArrayList<>();
ArrayList<Bar> l2=new ArrayList<>();
IntStream.range(0, Math.min(l1.size(), l2.size()))
         .mapToObj(i->new Pair(l1.get(i), l2.get(i)))
         . …

<小时>

¹(除非您直接实现 Spliterator)

这篇关于一起迭代两个 Java-8-Stream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:一起迭代两个 Java-8-Stream

基础教程推荐