If I have a Dataset<Foo>
where Foo
contains a method returning Optional<Bar>
How can I use the dataset map function to Bar
type, ignoring the missing values ?
If I do
dataset.map(x -> x.getOptionalValue().orElse(null), Encoders.bean(Foo.class))
The encoder is going to crash on null values so I can’t follow up with a filter(Objects::isNull)
I tried :
foos.map(x -> x.getOptionalValue(), Encoders.bean(Optional.class)).filter(Optional::isPresent).map(x -> (Foo) x.get(), Encoders.bean(Foo.class))
but the Filter is producing a Dataset<Optional<Object>>
, and so I have to cast at the get()
call.
Is there a better way ?