How to Convert temperature using stream in java ?

How to Convert temperature using stream in java

How to Convert temperature using stream in java

In this Java code snippet, a list of double values named `celsiusTemps` is created, containing temperatures in Celsius: 0.0, 10.0, 20.0, and 30.0. Using Java 8 streams, a new list called `fahrenheitTemps` is generated by applying a conversion formula to each Celsius temperature.

The `map` operation transforms each Celsius temperature `c` into its corresponding Fahrenheit equivalent by using the formula `(c * 9 / 5) + 32`. The `collect` operation then gathers these converted temperatures into the `fahrenheitTemps` list.

This results in a list containing the temperatures converted from Celsius to Fahrenheit.


List<Double> celsiusTemps = Arrays.asList(0.0, 10.0, 20.0, 30.0);
List<Double> fahrenheitTemps = celsiusTemps.stream()
.map(c -> c * 9 / 5 + 32)
.collect(Collectors.toList());

Leave a Reply

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