I have a collection of streams which i am trying to ZIP together into 1 stream. I am using guava Streams.zip method to zip the stream. It works fine when number of streams in collection is below 8000, above 8000 it starts throwing stack overflow exception. On locally debugging i have found that the stack overflow is happening inside zip method. It successfully zip until 8000 streams and starts to throw exception after that. I am not able to find a workaround this or to why is it happening. Need some help around this to find. The guava zip code is here https://github.com/google/guava/blame/6d7e326b2cbfba5f19fc67859c0b3d4c45fab63f/guava/src/com/google/common/collect/Streams.java#L318
I tried local debugging. Converted all my lambda calls to vanlla for loop , so to confirm we are not calling anything recursively.Finally pin pointed that is is being caused by zip function.
Source code:
merge method which uses zip.
private static <T> Stream<T> merge(Stream<T> firstList, Stream<T> secondList) {
return Streams.zip(firstList, secondList, (first, second) -> {
if (first == null) {
return second;
}
return first.merge(second);
});
}
I am calling the merge method as this
Collections.singletonList(inlineList.stream()
.reduce(merge)
where inline list is list of streams.
Exception:
java.lang.StackOverflowError at java.util.Spliterators$1Adapter.hasNext(Spliterators.java:681) at com.google.common.collect.Streams$1.tryAdvance(Streams.java:322) at java.util.Spliterators$1Adapter.hasNext(Spliterators.java:681) at com.google.common.collect.Streams$1.tryAdvance(Streams.java:322) at java.util.Spliterators$1Adapter.hasNext(Spliterators.java:681) at com.google.common.collect.Streams$1.tryAdvance(Streams.java:322) at java.util.Spliterators$1Adapter.hasNext(Spliterators.java:681) at com.google.common.collect.Streams$1.tryAdvance(Streams.java:322) at java.util.Spliterators$1Adapter.hasNext(Spliterators.java:681) at com.google.common.collect.Streams$1.tryAdvance(Streams.java:322) at java.util.Spliterators$1Adapter.hasNext(Spliterators.java:681) at com.google.common.collect.Streams$1.tryAdvance(Streams.java:322) at java.util.Spliterators$1Adapter.hasNext(Spliterators.java:681) at com.google.common.collect.Streams$1.tryAdvance(Streams.java:322) at java.util.Spliterators$1Adapter.hasNext(Spliterators.java:681) at com.google.common.collect.Streams$1.tryAdvance(Streams.java:322) at java.util.Spliterators$1Adapter.hasNext(Spliterators.java:681) at com.google.common.collect.Streams$1.tryAdvance(Streams.java:322) at java.util.Spliterators$1Adapter.hasNext(Spliterators.java:681)