一起迭代两个 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);
}
如果你想要并行压缩,你应该考虑 Stream
的 source.例如.您可以压缩两个 ArrayList
(或任何 RandomAccessList
),例如
If you want parallel capable zipping you should consider the source of the Stream
. E.g. you can zip two ArrayList
s (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
)
相关文章