I have some code which returns a Stream as intended, but maybe it can be replaced with some type of lambda or stream() operation instead of exhausting the iterators in a while
loop?
It is just a method that alternates elements from the streams first
and second
and stops when one of them runs out of elements.
public static <T>Stream<T> alternatingSequence(Stream<T> first, Stream<T> second){
Iterator<T> iteratorFirst = first.iterator();
Iterator<T> iteratorSecond = second.iterator();
Stream<T> resultStream = Stream.empty();
while (iteratorFirst.hasNext() && iteratorSecond.hasNext()){
resultStream = Stream.concat(resultStream, Stream.of(iteratorFirst.next(), iteratorSecond.next()));
}
return resultStream;
}
}