Understanding Optionals.orElse

13,894

Your usage of nested ifPresent calls is very similar to the (nested) forEach calls on streams, seen in a lot of other questions.

They are a clear sign that you should try better on functional thinking.

What you want to do is

System.out.println(
    Optional.ofNullable(userName) // will be an empty optional if userName is null
            .map(Name::getName)   // will turn to empty optional if getName returns null
            .map("Name is: "::concat) // prepend "Name is: " (only when we have a name)
            .orElse("No Name Found") // get the result string or the alternative
    );

The important point to understand, is, that the mapping steps applied to an empty Optional don’t do anything but return an empty Optional again. Since you want to print in either case, the print statement is not passed as consumer, but written as stand-alone statement, printing the value returned by the orElse invocation, which will be either, the non-null result of the processing steps or the alternative String.

Share:
13,894

Related videos on Youtube

silentsudo
Author by

silentsudo

Software Engineer

Updated on June 06, 2022

Comments

  • silentsudo
    silentsudo almost 2 years

    I am trying to learn Java 8 feature Optional. I am confused about how Optional.orElse is working. Here is what i have tried:

    public class OptionalsExample {
    
        public static void main(String[] args) {
            Name userName = new Name();
            userName.setName("John Doe");
            Optional<Name> optionalName = Optional.ofNullable(userName);
            optionalName.ifPresent(
                    (Name value) -> {
                        Optional<String> optionalNameString = Optional.ofNullable(value.getName());
                        optionalNameString.ifPresent(name -> System.err.println("Name is: " + optionalNameString.get()));
                    }
            );
        }
        private static void printError() {
            System.err.println("No Name present");
        }
    }
    

    My Concern is that if name is set everything works fine but when no name is set i want to execute orElse. I want to do something when i comment

    userName.setName("John Doe");
    

    like printing No Name Found

    How can i do that?

    TIA

    • Holger
      Holger almost 8 years
      When you write optionalNameString.ifPresent(name -> …), the actual String is already provided in the name parameter, so it makes no sense to invoke get() on the optionalNameString within the lambda expression. Similarly, it wouldn’t make any sense to invoke orElse at this place as the lambda expression won’t get executed when the Optional is empty.
  • silentsudo
    silentsudo almost 8 years
    what is the purpose of Optional if we r using ifs :p
  • silentsudo
    silentsudo almost 8 years
    @hogler m starting it , never too late ;) Thank you for explaination
  • Jude Niroshan
    Jude Niroshan almost 8 years
    @sector11 it avoids NPEs
  • garnet
    garnet about 3 years

Related