I think I'm going to answer the question.
return Optional.ofNullable(getMap()) .map(Map::entrySet) // gets the entryset .map(Stream::of) .orElseGet(Stream::empty) // i would then like to continue with .filter(e -> e.getKey().startsWith("f") .map(Entry::getValue) .findFirst();
I am sick, very sick when I saw the code above. Is it really important for you to write code freely and not write simple code? First of all, as @Didier L mentioned in the comments, this is the wrong way to use Optional . Secondly, code is so hard to read, isn't it? if you write it with a local variable definition:
Map<String, Integer> map = getMap(); return map == null ? Optional.<Integer> empty() : map.entrySet().stream() .filter(e -> e.getKey().startsWith("f")).map(Entry::getValue).findFirst();
Isn't that clear? Or you can do it with StreamEx if you cannot overcome without using a free approach:
StreamEx.ofNullable(getMap()) .flatMapToEntry(Function.identity()) .filterKeys(k -> k.startsWith("f")).values().findFirst();
Or my AbacusUtil library
EntryStream.of(getMap()).filterByKey(k -> k.startsWith("f")).values().first();
Always try to find the best approach if everything gets stuck.
source share