The mapToDouble()
method in Java, part of the java.util.stream.IntStream
interface, is used to apply a given function to each element of the stream, producing a new DoubleStream
of the results. This method is useful when you need to transform the elements of an IntStream
into double
values.
Table of Contents
- Introduction
mapToDouble()
Method Syntax- Understanding
mapToDouble()
- Examples
- Basic Usage
- Using
mapToDouble()
with Other Stream Operations
- Real-World Use Case
- Conclusion
Introduction
The mapToDouble()
method returns a DoubleStream
consisting of the results of applying a given function to the elements of the original IntStream
. This method is an intermediate operation, meaning it returns a new stream and does not modify the original stream.
mapToDouble() Method Syntax
The syntax for the mapToDouble()
method is as follows:
DoubleStream mapToDouble(IntToDoubleFunction mapper)
Parameters:
mapper
: AnIntToDoubleFunction
that represents the function to be applied to each element of the stream.
Returns:
- A new
DoubleStream
consisting of the results of applying the given function to the elements of the original stream.
Throws:
- This method does not throw any exceptions.
Understanding mapToDouble()
The mapToDouble()
method processes each element of the stream and applies the specified function to it, resulting in a new stream containing double
values. This is useful for performing operations that require floating-point arithmetic or conversions.
Examples
Basic Usage
To demonstrate the basic usage of mapToDouble()
, we will create an IntStream
and use mapToDouble()
to convert each integer to its corresponding double value.
Example
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
public class MapToDoubleExample {
public static void main(String[] args) {
IntStream intStream = IntStream.of(1, 2, 3, 4, 5);
// Use mapToDouble() to convert each integer to a double value
DoubleStream doubleStream = intStream.mapToDouble(n -> (double) n);
// Print the double values
doubleStream.forEach(System.out::println);
}
}
Output:
1.0
2.0
3.0
4.0
5.0
Using mapToDouble()
with Other Stream Operations
This example shows how to use mapToDouble()
in combination with other stream operations, such as filtering.
Example
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
public class MapToDoubleWithOtherOperationsExample {
public static void main(String[] args) {
IntStream intStream = IntStream.range(1, 10);
// Filter even numbers and map them to their square roots
DoubleStream processedStream = intStream.filter(n -> n % 2 == 0)
.mapToDouble(Math::sqrt);
// Print the processed elements
processedStream.forEach(System.out::println);
}
}
Output:
1.4142135623730951
2.0
2.449489742783178
2.8284271247461903
Real-World Use Case
Converting Prices to Discounts
In real-world applications, the mapToDouble()
method can be used to convert prices (integer values) to discounts (double values) based on a predefined discount rate.
Example
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
public class ConvertPricesExample {
public static void main(String[] args) {
IntStream prices = IntStream.of(100, 200, 300, 400, 500);
double discountRate = 0.1;
// Convert prices to discounts
DoubleStream discounts = prices.mapToDouble(price -> price * discountRate);
// Print the discounts
discounts.forEach(discount -> System.out.println("Discount: " + discount));
}
}
Output:
Discount: 10.0
Discount: 20.0
Discount: 30.0
Discount: 40.0
Discount: 50.0
Conclusion
The IntStream.mapToDouble()
method is used to apply a given function to each element of the stream, producing a new DoubleStream
of the results. This method is particularly useful for transforming the elements of an IntStream
into double
values. By understanding and using this method, you can efficiently manage and process streams of integer values in your Java applications, performing necessary transformations to double values when needed.
Comments
Post a Comment
Leave Comment