Converting between Stream Types – Streams

Converting between Stream Types

Table 16.2 provides a summary of interoperability between stream types—that is, transforming between different stream types. Where necessary, the methods are shown with the name of the built-in functional interface required as a parameter. Selecting a naming convention for method names makes it easy to select the right method for transforming one stream type to another.

Table 16.2 Interoperability between Stream Types

Stream typesTo Stream<R>To IntStreamTo LongStreamTo DoubleStream
From Stream<T>map(Function)mapToInt(ToIntFunction)mapToLong( ToLongFunction)mapToDouble(ToDoubleStream)
flatMap(Function)flatMapToInt(Function)flatMapToLong(Function)flatMapToDouble(Function)
From IntStreammapToObj(IntFunction)map(IntUnary-Operator)mapToLong(IntToLong-Function)mapToDouble(IntToDouble-Function)
Stream<Integer> boxed()flatMap(IntFunction)asLongStream()asDoubleStream()
From LongStreammapToObj(LongFunction)mapToInt(LongToInt-Function)map(DoubleUnary-Operator)mapToDouble(LongToDouble-Function)
Stream<Long> boxed() flatMap(DoubleFunction)asDoubleStream()
From DoubleStreammapToObj(DoubleFunction)mapToInt(DoubleToInt-Function)mapToLong(DoubleToLong-Function)map(DoubleUnary-Operator)
Stream<Double> boxed()  flatMap(DoubleFunction)
Mapping between Object Streams

The map() and flatMap() methods of the Stream<T> interface transform an object stream of type T to an object stream of type R. Examples using these two methods can be found in §16.5, p. 921, and §16.5, p. 924, respectively.

Mapping an Object Stream to a Numeric Stream

The mapToNumType() methods in the Stream<T> interface transform an object stream to a stream of the designated numeric type, where NumType is either Int, Long, or Double.

The query below sums the number of tracks for all CDs in a list. The mapToInt() intermediate operation at (2) accepts an IntFunction that extracts the number of tracks in a CD, thereby transforming the Stream<CD> created at (1) into an IntStream. The terminal operation sum(), as the name implies, sums the values in the IntStream (p. 973).

Click here to view code image

int totalNumOfTracks = CD.cdList
    .stream()                                         // (1) Stream<CD>
    .mapToInt(CD::noOfTracks)                         // (2) IntStream
    .sum();                                           // 42

The flatMapToNumType() methods are only defined by the Stream<T> interface to flatten a multilevel object stream to a numeric stream, where NumType is either Int, Long, or Double.

Earlier we saw an example of flattening a two-dimensional array using the flat-MapToInt() method (p. 924).

The query below sums the number of tracks for all CDs in two CD lists. The flatMapToInt() intermediate operation at (1) accepts a Function that maps each List<CD> in a Stream<List<CD>> to an IntStream whose values are the number of tracks in a CD contained in the list. The resulting Stream<IntStream> from the mapper function is flattened into an IntStream by the flatMapToInt() intermediate operation, thus transforming the initial Stream<List<CD>> into an IntStream. The terminal operation sum() sums the values in this IntStream (p. 973).

Click here to view code image

List<CD> cdList1 = List.of(CD.cd0, CD.cd1);
List<CD> cdList2 = List.of(CD.cd2, CD.cd3, CD.cd4);
int totalNumOfTracks =
    Stream.of(cdList1, cdList2)                       // Stream<List<CD>>
          .flatMapToInt(                              // (1)
              lst -> lst.stream()                     // Stream<CD>
                        .mapToInt(CD::noOfTracks))    // IntStream
                                                      // Stream<IntStream>,
                                                      //   flattened to IntStream.
          .sum();                                     // 42

Leave a Reply

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