Guidelines for writing processing when a value exists / does not exist in Java Optional

When writing the processing when the value exists / does not exist in Java Optional, the following three methods can be used.

I thought about my own guideline as to which method should be used in which case.

Specifications of each method

Optional.isPresent() ʻOptional.isPresent ()is a method that returnstrue if the value exists and false` if it does not. By using it in combination with the if ~ else statement, you can replace the conventional null check syntax almost as it is. However, such usage is also said to be ** "just replacing the null check, there is no point in using Optional" **.

final Optional<String> foo = Optional.ofNullable("foo");

if (foo.isPresent()) {
	System.out.println("foo=" + foo.orElse(""));
} else {
	System.out.println("foo is null");
}

Optional.ifPresent() ʻOptional.ifPresent ()` is a method that executes the function (Consumer) specified by the argument if the value exists. There is a restriction that the processing when the value does not exist cannot be described and ** variables outside the block cannot be rewritten in the function **.

final Optional<String> foo = Optional.ofNullable("foo");

foo.ifPresent(f -> System.out.println("foo=" + f));

Optional.ifPresentOrElse() ʻOptional.ifPresentOrElse ()` executes the first function (Consumer) specified by the argument if the value exists, and executes the second function (Runnable) specified by the argument if it does not exist. The method. You can describe the processing when the value does not exist, but there is a restriction that ** variables outside the block cannot be rewritten in the function **.

final Optional<String> foo = Optional.ofNullable("foo");

foo.ifPresentOrElse(f -> System.out.println("foo=" + f), () -> System.out.println("foo is null"));

flowchart

It is a flowchart of which method should be used in which case.

<!-TODO: Recreate the flowchart with draw.io-> https://qiita-image-store.s3.amazonaws.com/0/132608/149d5aab-1032-6390-c4b1-01f209874ec4.png

Basically, it seems good to use ʻOptional.ifPresent ()` and use other methods only when you want to rewrite variables outside the block or when you want to describe the processing when the value does not exist.

Reference link

Recommended Posts

Guidelines for writing processing when a value exists / does not exist in Java Optional
Handling when calling a key that does not exist in hash
Processing when an ID that does not exist in the database is entered in the URL
When seeking multiple in a Java array
Update if the document already exists in Azure Cosmos DB Java SDK, create new if it does not exist
Cause of is not visible when calling a method of another class in java
Escape processing when creating a URL in Ruby
Solution for NetBeans 8.2 not working in Java 9 environment
A note when you want Tuple in Java
You don't have to write for twice when you make a right triangle in Java
Make "I'm not a robot" in Java EE (Jakarta EE)
ConcurrentHashMap does not allow nulls for key and value
When Docker for Mac 2.4.0.0 does not reflect file changes
A note for Initializing Fields in the Java tutorial
[Java] When writing the source ... A memorandum of understanding ①
What I learned when building a server in Java
Correspondence when Ruby version does not switch in rbenv
Call a program written in Swift from Processing (Java)
When a breakpoint is set in IntelliJ IDEA but it does not stop in debugging [Gradle]
For those who have deleted a document in Firestore but the subcollection does not disappear
[Java] A technique for writing constructors, getters, and setters in one shot with IntelliJ IDEA.