diff --git a/core-java-modules/core-java-streams-2/src/test/java/com/baeldung/streams/StreamAddUnitTest.java b/core-java-modules/core-java-streams-2/src/test/java/com/baeldung/streams/StreamAddUnitTest.java index 1fada26de5..e80a4e431b 100644 --- a/core-java-modules/core-java-streams-2/src/test/java/com/baeldung/streams/StreamAddUnitTest.java +++ b/core-java-modules/core-java-streams-2/src/test/java/com/baeldung/streams/StreamAddUnitTest.java @@ -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,6 +44,16 @@ public class StreamAddUnitTest { assertEquals(resultList.get(3), (Double) 9.9); } + private static Stream insertInStream(Stream stream, T elem, int index) { + final Spliterator spliterator = stream.spliterator(); + final Iterator iterator = Spliterators.iterator(spliterator); + + return Stream.concat(Stream.concat( + Stream.generate(iterator::next).limit(index), + Stream.of(elem)), + StreamSupport.stream(spliterator, /* parallel= */ false)); + } + private Stream insertInStream(Stream stream, T elem, int index) { List result = stream.collect(Collectors.toList()); result.add(index, elem);