Stream.concat() method creates a concatenated stream in which the elements are all the elements of the first stream followed by all the elements of the second stream. The resulting stream is ordered if both of the input streams are ordered, and parallel if either of the input streams is parallel.
static <T> Stream<T> concat(Stream<? extends T> stream1, Stream<? extends T> stream2) Where, T is the type of stream elements, stream1 represents the first stream, stream2 represents the second stream and the function returns the concatenation of the two input streams
The calls to Stream.concat(stream1, stream2) can be think of as forming a binary tree. The concatenation of all the input streams is at the root. The individual input streams are at the leaves. Below given are some examples of trees upto four input streams a, b, c and d.
For two streams a and b, the tree looks like :
For three streams a, b and c, the tree looks like :
For four streams a, b, c and d, the tree looks like :
Each additional input stream adds one layer of depth to the tree and one layer of indirection to reach all the other streams.
Note : The elements returned by Stream.concat() method is ordered. For example, the following two lines returns the same result:
Stream.concat(Stream.concat(stream1, stream2), stream3); Stream.concat(stream1, Stream.concat(stream2, stream3));
But the result for the following two are different.
Stream.concat(Stream.concat(stream1, stream2), stream3); Stream.concat(Stream.concat(stream2, stream1), stream3);
Below are some examples to understand the implementation of the function in a better way.
Example 1 :
Geeks for GeeksQuiz GeeksforGeeks
Example 2 :
Geeks GeeksQuiz GeeksforGeeks GFG
Example 3 :
1520.0 1620.0 1720.0 1820.0
Example 4 :
Geeks for GeeksforGeeks GeeksQuiz
Example 5 :
1520 1620 1720 1820
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java.util.concurrent.RecursiveAction class in Java with Examples
- Java.util.function.IntPredicate interface in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Java.util.concurrent.Phaser class in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java Deprecated API Scanner tool (jdepscan) in Java 9 with Examples
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
- Java.util.function.BiPredicate interface in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.