How to use java Optional

How to declare Optional

To use Optional, wrap the type

Optional<String> opt1;

Same for homebrew class

testClass.java


public class testClass
{
	private String no;
	private BigDecimal val;
}
OPtional<testClass> opt2;

How to use

Use optional.of or optional.ofNullable to populate the optional type.

Optional<String> opt1 = Optional.of("test");

However, note that optional.of will cause an Exception if the argument is null.

Optional<String> opt1 = Optional.of(null);

Therefore, use optional.ofNullable.

Optional<String> opt1 = Optional.ofNullable("test");
Optional<String> opt2 = Optional.ofNullable(null);

testClass test = new testClass();
Optional<String> opt3 = Optional.ofNullable(test);

How to retrieve the value

To retrieve the value, use: get: If null, Exception occurs orElse: Returns the variable value if null does not appear, or returns the argument of orElse if null orElseGet: If null does not appear, variable value, if null, return the result of supplier

String val1 = opt1.get();
String val2 = opt1.orElse("")

When retrieving a value from your own class, you can get the field using: map

String val1 = opt3.map(testClass::getNo).orElse("1");
String val2 = opt3.map(v -> v.getNo()).orElse("2");

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]
How to use classes in Java?
[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
How to use rbenv
How to use letter_opener_web
How to use fields_for
How to use java.util.logging
How to use map
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)
How to use JUnit 5
[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