[Java] You might be happy if the return value of a method that returns null is Optional <>

Optional <> Eve

For example, suppose you create a method like this:

Factory.Java


public class Factory {
    /**
     *Creates and returns a new instance of MyObj.<br>
     *If creation fails, null is returned.
     *
     * @return
     */
    public MyObj createMyObj() {
      // ...
    }
}

The side that uses this method will have code like this.

python


Factory factory;
// ...
MyObj myObj = factory.createMyObj();

if (myObj != null) {
    //Processing on success
}

Optional <> Later night

I tried to return Optional <>.

Factory.Java


public class Factory {
    /**
     *Creates and returns a new instance of MyObj.<br>
     *If creation fails, null is returned.
     *
     * @return
     */
    public Optional<MyObj> createMyObj() {
      // ...
    }
}

The user side looks like this.

python


Optional<MyObj> myObj = factory.createMyObj();

myObj.ifPresent(o -> {
    //Processing on success
});

Recommended Posts

[Java] You might be happy if the return value of a method that returns null is Optional <>
Declare a method that has a Java return value with the return value data type
Sample program that returns the hash value of a file in Java
Determine that the value is a multiple of 〇 in Ruby
[Ruby] What happens if the method self is used as the return value?
[Java] JUnit that NG if a method with a large number of lines is detected using black magic
The order of Java method modifiers is fixed
[Java] use Optional instead of if (hoge! = null) {...}
Pass arguments to the method and receive the result of the operation as a return value
The end of catastrophic programming # 03 "Comparison of integers, if" a> b ", assume that it is" a --b> 0 ""
A memo of the program that allows you to realize that the probability of dice rolling is about 1/6
Create a method to return the tax rate in Java
A collection of patterns that you want to be aware of so as not to complicate the code
[Firebase] The problem that the currentUser of Firebase Auth does not become null even if the app is uninstalled.
Consider implementing a method that returns the difference between two Lists
[Java] List method that determines whether a specific object is included
Is the version of Elasticsearch you are using compatible with Java 11?
Investigation method when the CPU of the server running java is heavy
Summary: Implementation of a function that switches the logical value when a link is clicked [for own output]
About the matter that tends to be confused with ARG of Dockerfile which is a multi-stage build
A monad is a design pattern for "expressing the existence of side effects in a method with a return type".
A collection of phrases that impresses the "different feeling" of Java and JavaScript
To you who lament that the conversion of JODConverter + LibreOffice is slow
The content of the return value of executeBatch is different between 11g and 12c
Memo: [Java] If a file is in the monitored directory, process it.
[Java small story] Monitor when a value is added to the List
[Java] Check if the character string is composed only of blanks (= Blank)
A story that suffered from a space that does not disappear even if trimmed with Java The cause is BOM