Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Optional Double to Double in javafx

The following code if compiled gives the following error. How to solve this

error: incompatible types: OptionalDouble cannot be converted to Double .average();

  Double todaypctpnl = openPositionsdata.stream()
                                     .mapToDouble(c->Double.parseDouble(c.getTodaypctpnl()))
                                     .average();
like image 638
jay Avatar asked Aug 30 '25 16:08

jay


2 Answers

Did you take a look at the API docs for OptionalDouble?

There are several ways how to convert it back to a double value, like for example:

  • double value = OptionalDouble.orElse(-1)
  • double value = OptionalDouble.orElseThrow(IllegalStateException::new)

etc.

You will have to choose the one fitting best your current needs.

like image 50
eckig Avatar answered Sep 03 '25 00:09

eckig


https://docs.oracle.com/javase/tutorial/collections/streams/reduction.html

Double todaypctpnl = openPositionsdata.stream() .mapToDouble(c->Double.parseDouble(c.getTodaypctpnl())) .average().getAsDouble();

like image 34
Gregory Lopes Avatar answered Sep 03 '25 01:09

Gregory Lopes