I have a list of 'Blocks' that could be null. Each of the blocks contains a list of names that also could be null. I'm trying to find if any of the names in all the blocks is the word "blurb".
I have the following code which does work:
private boolean containsText(List<Blocks> blocks) {
return Optional.ofNullable(blocks)
.map(Collection::stream)
.map(o -> o.map(Blocks::getNames))
.map(e -> e.anyMatch(names -> names != null && names.contains("blurb")))
.orElse(false);
}
But since getNames could return null I have to check for it in the next statement. I could wrap it in another Optional at that point, but then I would end up with an
Optional<Stream<Optional<List<String>>>>
Using names -> names != null
seems cleaner? Or is there a way to simplify this?