In JDK 8 with lambda b93 there was a class java.util.stream.Streams.zip in b93 which could be used to zip streams (this is illustrated in the tutorial Exploring Java8 Lambda
Zipping two streams using JDK8 with lambda (gist).
public static Stream zip(Stream streamA, Stream streamB, BiFunction zipper) {
final Iterator iteratorA = streamA.iterator();
final Iterator iteratorB = streamB.iterator();
final Iterator iteratorC = new Iterator() {
@Override
public boolean hasNext() {
return iteratorA.hasNext() && iteratorB.hasNext();
}
@Override
public C next() {
return zipper.apply(iteratorA.next(), iteratorB.next());
}
};
final boolean parallel = streamA.isParallel() || streamB.isParallel();
return iteratorToFiniteStream(iteratorC, parallel);
}
public static Stream iteratorToFiniteStream(Iterator iterator, boolean parallel) {
final Iterable iterable = () -> iterator;
return StreamSupport.stream(iterable.spliterator(), parallel);
}