I wonder if there is some alternative way for filtering and unwrapping Optional when using streams in Java 8 than I normally use.
Here is a snippet of some code that uses Optional as part of file error handling when producing some markdown, which might fail on an IO exception, which I'm handling with a failed list.
files.stream()
.map(f -> {
try {
return Optional.of(generateMarkdown(f));
} catch (IOException e) {
failed.add(f);
}
return Optional.<Markdown>empty();
})
.filter(o -> o.isPresent())
.map(o -> o.get())
.forEach(h -> { ...
Is there a better way to write the .filter
and .map
lines, or to rewrite the .map block not to need them?