Merge pull request #12556 from GeoffChurch/insertInStream

Improve `insertInStream`
This commit is contained in:
Loredana Crusoveanu 2022-09-08 10:35:33 +03:00 committed by GitHub
commit b8bab5bcbe
1 changed files with 12 additions and 4 deletions

View File

@ -2,9 +2,13 @@ package com.baeldung.streams;
import org.junit.Test;
import java.util.Iterator;
import java.util.List;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static org.junit.Assert.assertEquals;
@ -40,9 +44,13 @@ public class StreamAddUnitTest {
assertEquals(resultList.get(3), (Double) 9.9);
}
private <T> Stream<T> insertInStream(Stream<T> stream, T elem, int index) {
List<T> result = stream.collect(Collectors.toList());
result.add(index, elem);
return result.stream();
private static <T> Stream<T> insertInStream(Stream<T> stream, T elem, int index) {
final Spliterator<T> spliterator = stream.spliterator();
final Iterator<T> iterator = Spliterators.iterator(spliterator);
return Stream.concat(Stream.concat(
Stream.generate(iterator::next).limit(index),
Stream.of(elem)),
StreamSupport.stream(spliterator, /* parallel= */ false));
}
}