Concatenating Streams – Streams
Concatenating Streams
The concat() method creates a resulting stream where the elements from the first argument stream are followed by the elements from the second argument stream. The code below illustrates this operation for two unordered sequential streams. Two sets are created at (1) and (2) based on lists of strings that are passed to the set constructors. The two streams created at (3) and (4) are unordered, since they are created from sets (p. 897). These unordered streams are passed to the concat() method at (5). The resulting stream is processed in the pipeline comprising (5) and (6). The forEachOrdered() operation at (6) respects the encounter order of the stream if it has oneāthat is, if it is ordered (p. 948). The output confirms that the resulting stream is unordered.
Set<String> strSet1 // (1)
= Set.of(“All”, ” objects”, ” are”, ” equal”);
Set<String> strSet2 // (2)
= Set.of(” but”, ” some”, ” are”, ” more”, ” equal”, ” than”, ” others.”);
Stream<String> unorderedStream1 = strSet1.stream(); // (3)
Stream<String> unorderedStream2 = strSet2.stream(); // (4)
Stream.concat(unorderedStream1, unorderedStream2) // (5)
.forEachOrdered(System.out::print); // (6)
// objectsAll equal are some are others. than equal more but
The resulting stream is ordered if both argument streams are ordered. The code below illustrates this operation for two ordered sequential streams. The two streams created at (1) and (2) below are ordered. The ordering is given by the specification order of the strings as arguments to the Stream.of() method. These ordered streams are passed to the concat() method at (3). The resulting stream is processed in the pipeline comprising (3) and (4). The output confirms that the resulting stream is ordered.
Stream<String> orderedStream1 = Stream.of(“All”, ” objects”, // (1)
” are”, ” equal”);
Stream<String> orderedStream2 = Stream.of(” but”, ” some”, ” are”, ” more”, // (2)
” equal”, ” than”, ” others.”);
Stream.concat(orderedStream1, orderedStream2) // (3)
.forEachOrdered(System.out::print); // (4)
// All objects are equal but some are more equal than others.
As far as the mode of the resulting stream is concerned, it is parallel if at least one of the constituent streams is parallel. The code below illustrates this behavior.
The parallel() intermediate operation used at (1) returns a possibly parallel stream (p. 933). The call to the isParallel() method confirms this at (2). We pass one parallel stream and one sequential stream to the concat() method at (3). The call to the isParallel() method at (4) confirms that the resulting stream is parallel. The printout from (5) shows that it is also unordered. Note that new streams are created on the sets strSet1 and strSet2 at (1) and (3), respectively, as we cannot reuse the streams that were created earlier and consumed.
Stream<String> pStream1 = strSet1.stream().parallel(); // (1)
System.out.println(“pStream1 is parallel: ” + pStream1.isParallel()); // (2) true
Stream<String> rStream = Stream.concat(pStream1, strSet2.stream()); // (3)
System.out.println(“rStream is parallel: ” + pStream1.isParallel()); // (4) true
rStream.forEachOrdered(System.out::print); // (5)
// objectsAll equal are some are others. than equal more but
Archives
- July 2024
- June 2024
- May 2024
- March 2024
- February 2024
- January 2024
- December 2023
- October 2023
- September 2023
- May 2023
- March 2023
- January 2023
- December 2022
- November 2022
- October 2022
- September 2022
- August 2022
- July 2022
- April 2022
- March 2022
- November 2021
- October 2021
- September 2021
- July 2021
- June 2021
- March 2021
- February 2021
Calendar
M | T | W | T | F | S | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |