Adding an Element to a Java Stream

Learn how to add an element, in the beginning, end, or specific index position in a Java Stream.

Overview

This article provides quick examples of appending, prepending or inserting a single element into a Java Stream instance.

Append an Element to a Java Stream

Java Stream API provides a static method, ‘concat(stream1, stream2)‘, that can concatenate two different Java Streams. We can use this to append – add in the end – an element to a Java Stream.

Example of Appending a Single Element in a Java Stream

Stream<String> stream = Stream.of("a", "b", "c");        
Stream<String> newStream = Stream
    .concat(stream, Stream.of("d"));
 
newStream.forEach(System.out::print);

//prints: 
//abcdCode language: Java (java)

Please note that if the first Stream has infinite elements, the element may never get appended.

Prepending an Element to a Java Stream

Prepending is adding something in the beginning. To add an element at the beginning of a Stream, we can use the ‘concat(stream1, stream2)‘ method.

Stream<String> stream = Stream.of("a", "b", "c");        
Stream<String> newStream = Stream
    .concat(Stream.of("z"), stream);
 
newStream.forEach(System.out::print);

//prints: 
//zabcCode language: Java (java)

Adding an Element to a Specific Index in Java Stream

A Java Stream represents a sequential flow of finite or infinite elements, and it is lazy in that it processes elements only when a terminal operator demands them. Although the Steams API doesn’t provide a direct way of inserting a new element at a particular position in a Stream, we can use the following two workarounds to achieve that.

By Collecting the Stream as a Java Collection

Java Lists provide a way to insert an element into a specific index position. Thus, we can collect all the elements of a Stream into a list, add an element to a particular index position, and create a new Stream.

Example of Using a List to insert an element into a Java Stream

Stream<Integer> stream = Stream.of(1, 2, 6, 7);

List<T> list = stream.collect(toList());
list.add(2, 3);
        
Stream<Integer> newStream = list.stream();

newStream.forEach(System.out::print);
//prints:
//12367Code language: Java (java)

Please note that when we collect a Stream into a List, all the elements of the Stream are processed eagerly, and we won’t get any benefit from the Stream laziness.

By Using Spliterator

Alternatively, we can take Stream Spliterator‘s help to split a Stream at a particular index position and the ‘concat()‘ method to append the new element and then append the rest of the Stream.

Example of Using Stream Spliterator to insert an element at a specific index position

Stream<Integer> stream = Stream.of(1, 2, 6, 7);

Spliterator<T> spliterator = stream.spliterator();        
Iterator<T> iterator = Spliterators.iterator(spliterator);

Stream<T> stream1 = Stream.concat(
    Stream.generate(iterator::next).limit(2), 
    Stream.of(3));
Stream<T> stream2 = StreamSupport.stream(spliterator, false);

Stream<Integer> newStream = Stream.concat(stream1, stream2);

newStream.forEach(System.out::print);
//prints:
//12367Code language: Java (java)

We used Spliterator and the ‘limit()‘ method to split the Stream from the beginning to the specific index position. We then appended the element to the first stream. Later we concatenated the first stream to the remaining part of the Stream to get the desired output.

Summary

This article demonstrated examples of adding an element at the beginning, at the end, or at a specific index position of a Java Stream.

You can refer to our GitHub Repository for the complete source code of the examples used in this tutorial.