Mapping between Numeric Streams – Streams

Mapping between Numeric Streams

In contrast to the methods in the Stream<T> interface, the map() and the flatMap() methods of the numeric stream interfaces transform a numeric stream to a numeric stream of the same primitive type; that is, they do not change the type of the numeric stream.

The map() operation in the stream pipeline below does not change the type of the initial IntStream.

Click here to view code image

IntStream.rangeClosed(1, 3)                           // IntStream
         .map(i -> i * i)                             // IntStream
         .forEach(n -> System.out.printf(“%d “, n));  // 1 4 9

The flatMap() operation in the stream pipeline below also does not change the type of the initial stream. Each IntStream created by the mapper function is flattened, resulting in a single IntStream.

Click here to view code image

IntStream.rangeClosed(1, 3)                           // IntStream
         .flatMap(i -> IntStream.rangeClosed(1, 4))   // IntStream
         .forEach(n -> System.out.printf(“%d “, n));  // 1 2 3 4 1 2 3 4 1 2 3 4

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

The mapToDouble() operation in the stream pipeline below transforms the initial IntStream into a DoubleStream.

Click here to view code image

IntStream.rangeClosed(1, 3)                           // IntStream
         .mapToDouble(i -> Math.sqrt(i))              // DoubleStream
         .forEach(d -> System.out.printf(“%.2f “, d));// 1.00 1.41 1.73

The methods asLongStream() and asDoubleStream() in the IntStream interface transform an IntStream to a LongStream and a DoubleStream, respectively. Similarly, the method asDoubleStream() in the LongStream interface transforms a LongStream to a DoubleStream.

The asDoubleStream() operation in the stream pipeline below transforms the initial IntStream into a DoubleStream. Note how the range of int values is thereby transformed to a range of double values by the asDoubleStream() operation.

Click here to view code image

IntStream.rangeClosed(1, 3)                           // IntStream
         .asDoubleStream()                            // DoubleStream
         .map(d -> Math.sqrt(d))                      // DoubleStream
         .forEach(d -> System.out.printf(“%.2f “, d));// 1.00 1.41 1.73

In the stream pipeline below, the int values in the IntStream are first boxed into Integers. In other words, the initial IntStream is transformed into an object stream, Stream<Integer>. The map() operation transforms the Stream<Integer> into a Stream<Double>. In contrast to using the asDoubleStream() in the stream pipeline above, note the boxing/unboxing that occurs in the stream pipeline below in the evaluation of the Math.sqrt() method, as this method accepts a double as a parameter and returns a double value.

Click here to view code image

IntStream.rangeClosed(1, 3)                           // IntStream
         .boxed()                                     // Stream<Integer>
         .map(n -> Math.sqrt(n))                      // Stream<Double>
         .forEach(d -> System.out.printf(“%.2f “, d));// 1.00 1.41 1.73

Leave a Reply

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