[Solved] Is there a way to combine Java8 Optional returning a value with printing a message on null?


Use orElseGet:

Optional<String> startingOptional = getMyOptional();
String finishingValue = startingOptional.orElseGet(() -> {
    System.out.println("value not found");
    return "";
});

Using .map(value -> value) is useless: transforming a value into itself doesn’t change anything.

solved Is there a way to combine Java8 Optional returning a value with printing a message on null?