[Java] use Optional instead of if (hoge! = null) {...}

I didn't know how to use Optional, but I understood that it was something like this.

I want to execute the method of hoge only when the variable hoge is not null

Conventional way

python


if (hoge != null) {
    hoge.fuga();
}

How to use ʻOptional`

python


Optional
    .ofNullable(hoge)
    .ifPresent(o -> o.fuga());

Which one is easier to see? : smile:

If the variable hoge is not null, the method execution result of hoge is returned.

Conventional way

python


fugo = hoge != null ? hoge.fugo() : null;

How to use ʻOptional`

python


fugo = Optional
    .ofNullable(hoge)
    .map(o -> o.fugo())  // Optional<?>Will be returned, so
    .orElse(null);       //Make orElse return the value itself or the default value (null here)

Is it easier to see the ternary operator at this level? : smile:

If the variable hoge is not null, call the method of hoge, and if the result of the method is not null, call the method further.

I don't know what it says. Look at the code.

Conventional way

python


instant = null;

if (hoge != null) {
    date = hoge.getDate();

    if (date != null) {
        instant = date.toInstant();
    }
}

Or how about this?

python


instant = hoge != null
    ? (hoge.getDate() != null
        ? hoge.getDate().toInstant()
        : null) 
    : null;

How to use ʻOptional`

instant = Optional
  .ofNullable(hoge)
  .map(o -> o.getDate())     
  .map(d -> d.toInstant())   
  .orElse(null);

Which one is easier to see? : smile:

Recommended Posts

[Java] use Optional instead of if (hoge! = null) {...}
[Java] Why use StringUtils.isEmpty () instead of String.isEmpty ()
Why use setters/getters instead of public/private in Java
[Null safety] Kotlin Java comparison memo Correct use of Null safety
How to use java Optional
[Java] How to use Optional ②
[Java] How to use Optional ①
Use enum instead of int constant
Basic usage of java Optional Part 1
[Java] You might be happy if the return value of a method that returns null is Optional <>
Item 36: Use EnumSet instead of bit fields
Item 37: Use EnumMap instead of ordinal indexing
[Java] [Maven3] Summary of how to use Maven3
[Java] Learn how to use Optional correctly
Basics of java basics ② ~ if statement and switch statement ~
Use jenv to enable multiple versions of Java
[Java] Use of final in local variable declaration
Java Optional type
Studying Java 8 (Optional)
JAVA NULL checker
[Java] Use Collectors.collectingAndThen
[Java] Overview of Java
[Java] Optional memorandum
Java 9 Optional :: stream
[Java] How to use compareTo method of Date class
Omission of curly braces in if statement (Java silver)
[Swift] Why FlowLayout should use Delegate instead of instance
Use Java lambda expressions outside of the Stream API
Browse class objects in Kotlin (instead of Java class name.class)
Summary of Java communication API (1) How to use Socket
Summary of Java communication API (3) How to use SocketChannel
Summary of Java communication API (2) How to use HttpUrlConnection
Use of Abstract Class and Interface properly in Java
Java: Use Stream to sort the contents of the collection
JAWJAW is convenient if you use WordNet from Java
Summarize the additional elements of the Optional class in Java 9