How to interleave (merge) two Java 8 Streams?(如何交错(合并)两个 Java 8 流?)
问题描述
流<字符串>a = Stream.of("一", "三", "五");流<字符串>b = Stream.of("二", "四", "六");我需要做什么才能使输出如下?
//一个//二//三//四个//五//六我查看了 concat 但正如 javadoc 解释的那样,它只是一个接一个地附加,它不会交错/穿插.
流<字符串>out = Stream.concat(a, b);out.forEach(System.out::println);<块引用>
创建一个惰性连接流,其元素都是第一个流的元素,然后是第二个流.
错误地给予
//一个//三//五//二//四个//六如果我收集它们并进行迭代可以做到,但希望有更多的 Java8-y、Streamy :-)
注意
我不想压缩流
<块引用>zip"操作将从每个集合中取出一个元素并将它们组合起来.
zip 操作的结果是这样的:(不需要的)
//onetwo//三四//五六我会使用这样的:
公共静态<T>流<T>interleave(Stream 扩展 T>a,Stream 扩展 T>b) {分离器动作){分离器它尽可能地保留了输入流的特征,这允许进行某些优化(例如对于count()和toArray()).此外,即使输入流可能是无序的,它也会添加 ORDERED 以反映交错.
当一个流的元素多于另一个时,剩余的元素将出现在最后.
Stream<String> a = Stream.of("one", "three", "five");
Stream<String> b = Stream.of("two", "four", "six");
What do I need to do for the output to be the below?
// one
// two
// three
// four
// five
// six
I looked into concat but as the javadoc explains, it just appends one after the other, it does not interleave / intersperse.
Stream<String> out = Stream.concat(a, b);
out.forEach(System.out::println);
Creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream.
Wrongly gives
// one
// three
// five
// two
// four
// six
Could do it if I collected them and iterated, but was hoping for something more Java8-y, Streamy :-)
Note
I don't want to zip the streams
"zip" operation will take an element from each collection and combine them.
the result of a zip operation would be something like this: (unwanted)
// onetwo
// threefour
// fivesix
I’d use something like this:
public static <T> Stream<T> interleave(Stream<? extends T> a, Stream<? extends T> b) {
Spliterator<? extends T> spA = a.spliterator(), spB = b.spliterator();
long s = spA.estimateSize() + spB.estimateSize();
if(s < 0) s = Long.MAX_VALUE;
int ch = spA.characteristics() & spB.characteristics()
& (Spliterator.NONNULL|Spliterator.SIZED);
ch |= Spliterator.ORDERED;
return StreamSupport.stream(new Spliterators.AbstractSpliterator<T>(s, ch) {
Spliterator<? extends T> sp1 = spA, sp2 = spB;
@Override
public boolean tryAdvance(Consumer<? super T> action) {
Spliterator<? extends T> sp = sp1;
if(sp.tryAdvance(action)) {
sp1 = sp2;
sp2 = sp;
return true;
}
return sp2.tryAdvance(action);
}
}, false);
}
It retains the characteristics of the input streams as far as possible, which allows certain optimizations (e.g. for count()and toArray()). Further, it adds the ORDERED even when the input streams might be unordered, to reflect the interleaving.
When one stream has more elements than the other, the remaining elements will appear at the end.
这篇关于如何交错(合并)两个 Java 8 流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何交错(合并)两个 Java 8 流?
基础教程推荐
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
