Following code is throwing NPE for the property Salary being null.
class Person has properties: string: name, Integer: age, Integer: salary
salary
can be null here. I want to create a List of salaries.
persons.stream().mapToDouble(Person::getSalary).boxed().collect(Collectors.toList())
Here I must retain null values in the result list. null can not be replaced with 0.
I think you could use map
instead of mapToDouble
along with the ternary operator:
List<Double> salaries = persons.stream()
.map(Person::getSalary)
.map(s -> s == null ? null : s.doubleValue())
.collect(Collectors.toList())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With