2 ms·
The closest equivalent would be something like: Optional<Integer> num = getSomeRiskyNumber(); return num.map(x -> ...).orElseGet(return error or whatever);
by andrewaylett 10y ago
The closest equivalent would be something like:
Optional<Integer> num = getSomeRiskyNumber();
return num.map(x -> ...).orElseGet(return error or whatever);
Which is just as type-safe. There are a number of variants, including flatMap for when the map operation itself returns an Optional (the monadic bind operator) and a number of options for how to deal with the empty case.