1. Overview

In this quick article, we’ll look at how to add an element to a Java 8 Stream, which is not as intuitive as adding an element to a normal collection.

2. Prepending

We can easily prepend a given element to a Stream by invoking the static Stream.concat() method:

@Test
public void givenStream_whenPrependingObject_thenPrepended() {
    Stream<Integer> anStream = Stream.of(1, 2, 3, 4, 5);

    Stream<Integer> newStream = Stream.concat(Stream.of(99), anStream);

    assertEquals(newStream.findFirst().get(), (Integer) 99);
}

3. Appending

Likewise, to append an element to the end of a Stream, we just need to invert the arguments.

Keep in mind that Streams can represent infinite sequences, so there are scenarios when you might never get to your new element:

@Test
public void givenStream_whenAppendingObject_thenAppended() {
    Stream<String> anStream = Stream.of("a", "b", "c", "d", "e");

    Stream<String> newStream = Stream.concat(anStream, Stream.of("A"));

    List<String> resultList = newStream.collect(Collectors.toList());
 
    assertEquals(resultList.get(resultList.size() - 1), "A");
}

4. At a Specific Index

Stream (java.util.stream) in Java is a sequence of elements supporting sequential and parallel aggregate operations. There is no function for adding a value to a specific index since it’s not designed for such a thing. However, there are a couple of ways to achieve it.

One approach is to use a terminal operation to collect the values of the stream to an ArrayList and then simply use add(int index, E element) method. Keep in mind that this will give you the desired result, but you will also lose the laziness of a Stream because you need to consume it before inserting a new element.

The alternative is to use Spliterator and iterate the elements sequentially to the target index (using the iterator of the Spliterator class.) The concept is to break the stream into two streams (A and B). Stream A starts from index 0 to the target index, and stream B is the remaining element. Then add the element to stream A and concatenate streams A and B.

In this case, since the stream is not consumed greedily, it is fully lazy; you can insert an element at a given index in constant time:

private static  Stream insertInStream(Stream stream, T elem, int index) {
    Spliterator spliterator = stream.spliterator();
    Iterator iterator = Spliterators.iterator(spliterator);

    return Stream.concat(Stream.concat(Stream.generate(iterator::next)
      .limit(index), Stream.of(elem)), StreamSupport.stream(spliterator, false));
}

Now, let’s test our code to ensure everything is working as expected:

@Test
public void givenStream_whenInsertingObject_thenInserted() {
    Stream<Double> anStream = Stream.of(1.1, 2.2, 3.3);
    Stream<Double> newStream = insertInStream(anStream, 9.9, 3);

    List<Double> resultList = newStream.collect(Collectors.toList());
 
    assertEquals(resultList.get(3), (Double) 9.9);
}

5. Conclusion

In this short article, we’ve seen how to add a single element to a Stream, be it at the beginning, at the end, or at a given position.

Prepending an element works for any Stream and technically works to append to the end of an infinite Stream.

As always, the complete source code can be found on Github.