Java DoubleStream mapToLong() Method

The mapToLong() method in Java, part of the java.util.stream.DoubleStream interface, is used to apply a given function to each element of the stream, producing a LongStream with the results of the applied function. This method is useful when you need to transform the elements of a DoubleStream into long values.

Table of Contents

  1. Introduction
  2. mapToLong() Method Syntax
  3. Understanding mapToLong()
  4. Examples
    • Basic Usage
    • Using mapToLong() with Custom Transformation
  5. Real-World Use Case
  6. Conclusion

Introduction

The mapToLong() method returns a LongStream consisting of the results of applying a given function to the elements of the original DoubleStream. This method is an intermediate operation, meaning it returns a new stream and does not modify the original stream.

mapToLong() Method Syntax

The syntax for the mapToLong() method is as follows:

LongStream mapToLong(DoubleToLongFunction mapper)

Parameters:

  • mapper: A DoubleToLongFunction that represents the function to be applied to each element of the stream.

Returns:

  • A LongStream consisting of the results of applying the given function to the elements of the original DoubleStream.

Throws:

  • This method does not throw any exceptions.

Understanding mapToLong()

The mapToLong() method allows you to transform the elements of a DoubleStream by applying a specified function to each element. The resulting stream contains the transformed elements as long values, preserving the order of the original stream.

Examples

Basic Usage

To demonstrate the basic usage of mapToLong(), we will create a DoubleStream and use mapToLong() to convert each element to its long part.

Example

import java.util.stream.DoubleStream;
import java.util.stream.LongStream;

public class MapToLongExample {
    public static void main(String[] args) {
        DoubleStream doubleStream = DoubleStream.of(1.1, 2.2, 3.3, 4.4, 5.5);

        // Apply a function to convert each element to its long part
        LongStream longStream = doubleStream.mapToLong(n -> (long) n);

        // Print the resulting LongStream
        longStream.forEach(System.out::println);
    }
}

Output:

1
2
3
4
5

Using mapToLong() with Custom Transformation

This example shows how to use mapToLong() to transform a DoubleStream into a LongStream by applying a custom transformation. In this case, we will multiply each element by 100 and convert it to a long.

Example

import java.util.stream.DoubleStream;
import java.util.stream.LongStream;

public class CustomTransformationExample {
    public static void main(String[] args) {
        DoubleStream doubleStream = DoubleStream.of(1.1, 2.2, 3.3, 4.4, 5.5);

        // Apply a custom function to multiply each element by 100 and convert to long
        LongStream longStream = doubleStream.mapToLong(n -> (long) (n * 100));

        // Print the resulting LongStream
        longStream.forEach(System.out::println);
    }
}

Output:

110
220
330
440
550

Real-World Use Case

Converting Sensor Readings to Long Units

In real-world applications, the mapToLong() method can be used to convert sensor readings from double values to long units for further processing or storage.

Example

import java.util.stream.DoubleStream;
import java.util.stream.LongStream;

public class SensorDataConversionExample {
    public static void main(String[] args) {
        DoubleStream sensorData = DoubleStream.of(25.3, 26.7, 24.8, 27.5, 30.1);

        // Convert the sensor readings to long units
        LongStream longSensorData = sensorData.mapToLong(reading -> (long) reading);

        // Print the converted sensor readings
        longSensorData.forEach(longReading -> System.out.println("Long sensor reading: " + longReading));
    }
}

Output:

Long sensor reading: 25
Long sensor reading: 26
Long sensor reading: 24
Long sensor reading: 27
Long sensor reading: 30

Conclusion

The DoubleStream.mapToLong() method is used to apply a given function to each element of the stream, producing a LongStream with the results of the applied function. This method is particularly useful for transforming the elements of a DoubleStream into long values. By understanding and using this method, you can efficiently manage and manipulate streams of double values in your Java applications.

Comments