我有一个需要的案例
map一个对象,如果映射函数抛出异常,我将其映射到null.filternull对象的映射流,如果为null则抛出异常,否则收集到List.我怎么做到这一点?
list.stream().map(ob-> {
try {
// cannot throw only catch
return function(ob);
} catch (Exception e) {
log.error(e);
return null;
}
}).filter(Objects::isNull).findFirst().orElseThrow(Exception::new);
Run Code Online (Sandbox Code Playgroud)
现在我的问题是我应该如何将上述lambda调整/重构为throw new Exception()on null或者collect(Collectors.toList()).