Insert an element into a Stream in Java
This article will discuss how to insert an element to a stream in Java either at the beginning or the end of the stream.
1. Inserting at the beginning
The idea is to create a sequential stream of the given element using the Stream.of() factory method and then use Stream.concat() for concatenating the resultant stream into the original stream.
Please note that to insert the given element at the beginning, the resultant stream from that element is passed as the first parameter to the Stream.concat() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Arrays; import java.util.List; import java.util.stream.Stream; class Main { // Generic method to insert an element at the beginning of a stream public static<T> Stream<T> addToBeg(Stream<T> stream, T element) { return Stream.concat(Stream.of(element), stream); } public static void main(String[] args) { Stream<Integer> source = Stream.of(1, 2, 3, 4); source = addToBeg(source, 0); System.out.println(Arrays.toString(source.toArray())); } } |
Output:
[0, 1, 2, 3, 4]
We can also convert the stream to a list, insert the specified element into the list initially, and then get back the stream from that list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; class Main { // Generic method to insert an element at the beginning of a stream public static<T> Stream<T> addToBeg(Stream<T> stream, T element) { List<T> result = stream.collect(Collectors.toList()); result.add(0, element); return result.stream(); } public static void main(String[] args) { Stream<Integer> source = Stream.of(1, 2, 3, 4); source = addToBeg(source, 0); System.out.println(Arrays.toString(source.toArray())); } } |
Output:
[0, 1, 2, 3, 4]
2. Inserting at the end
To insert an element at the end of a stream, we need to change the order in which the arguments were passed to the Stream.concat() method. Now the resultant stream from the given element is passed as the second parameter to the Stream.concat() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Arrays; import java.util.List; import java.util.stream.Stream; class Main { // Generic method to insert an element at the end of a stream public static<T> Stream<T> addToEnd(Stream<T> stream, T element) { return Stream.concat(stream, Stream.of(element)); } public static void main(String[] args) { Stream<Integer> source = Stream.of(1, 2, 3, 4); source = addToEnd(source, 5); System.out.println(Arrays.toString(source.toArray())); } } |
Output:
[1, 2, 3, 4, 5]
Please note that this approach will only work for finite streams. For streams representing an infinite sequence, we’ll never get to the end of it. This is demonstrated here.
We can also convert the stream to a list and insert the given element similar to the earlier approach, but even that doesn’t help in infinite streams.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; class Main { // Generic method to insert an element at the end of a stream public static<T> Stream<T> addToEnd(Stream<T> stream, T element) { List<T> result = stream.collect(Collectors.toList()); result.add(element); return result.stream(); } public static void main(String[] args) { Stream<Integer> source = Stream.of(1, 2, 3, 4); source = addToEnd(source, 5); System.out.println(Arrays.toString(source.toArray())); } } |
Output:
[1, 2, 3, 4, 5]
That’s all about inserting an element into a Stream in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)