[Java] How to use Optional ①

Programming study diary

January 1, 2021 A brief summary of how to use Java Optional.

What is Optional

Use this when you want to explicitly indicate that null may be returned as the return value of the method. It can be implemented more safely by showing that the method can return null.

How to write Optional

You need to import java.util.Optional to use the Optional class. Generally, an object is defined and used. Optional objects are declared by specifying the type of the object that may be null in the Type Parameter without the diamond operator ('<>'). Specify the object name that may be null in the argument of the ofNullable method.

Basic writing


Optional<T>Object name= Optional.ofNullable(Object name that may be null);

Sample code


String str = null;
Optional<String> value = Optional.ofNullable(str);

How to use the or method

The or method executes processing only when the object specified in the argument of the Optional.ofNullable method is null.

Sample code


import java.util.Optional;
 
public class Sample {
    public static void main(String[] args) {
        String str = null;
        Optional<String> value = Optional.ofNullable(str);
        System.out.println(value.or(() -> Optional.of("null")).get());
    }
}

Execution result


null

How to use the orEsle method

If the argument of Optional.ofNullable method is null, the value is returned to the argument of orElse method. If it is not null, it returns its non-null value.

Sample code


import java.util.Optional;
 
public class Sample {
    public static void main(String[] args) {
        String str = null;
        Optional<String> value = Optional.ofNullable(str);
        str = value.orElse("null");
        System.out.println(str);
    }
}

Execution result


null

References

How to use Optional class in Java [For beginners] What is Java Optiona? Easy-to-understand explanation of how to use each pattern

Recommended Posts

How to use java Optional
[Java] How to use Optional ②
[Java] How to use Optional ①
[Java] Learn how to use Optional correctly
[Java] How to use Map
[Java] How to use Map
How to use java class
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
How to use Java variables
How to use Java HttpClient (Get)
How to use Java HttpClient (Post)
[Java] How to use join method
[Processing × Java] How to use variables
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
[Processing × Java] How to use arrays
How to use Java lambda expressions
[Java] How to use Math class
How to use Java enum type
Multilingual Locale in Java How to use Locale
How to use Map
[Java] How to use the File class
How to use rbenv
[Java] How to use the hasNext function
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use submit method (Java Silver)
[Java] How to use the HashMap class
How to use collection_select
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use Java classes, definitions, import
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
How to use Java Scanner class (Note)
[Processing × Java] How to use the function
[Easy-to-understand explanation! ] How to use ArrayList [Java]
How to use Dozer.mapper
[Java] How to use the Calendar class
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
[Easy-to-understand explanation! ] How to use Java overload