Iterate two Java-8-Streams together(一起迭代两个 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
)
这篇关于一起迭代两个 Java-8-Stream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:一起迭代两个 Java-8-Stream
基础教程推荐
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01