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.

Click here to view code image

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.

Click here to view code image

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.

Click here to view code image

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

Leave a Reply

Your email address will not be published. Required fields are marked *