Basic usage of java Optional Part 1

What is Optional? It is a memorandum article for myself at that time. As a rough idea, the optional type is a type that you can see at a glance that "null may be included". First, a simple null check example

		if (name != null) {
			System.out.println(name);
		} else {
			System.out.println("I don't have a name");
		}

Of course the result

//to name"I have a name"When assigned to
I have a name

//If name is null
I don't have a name

If you do this with Optional, it looks like this

		Optional.ofNullable(name).ifPresentOrElse(result -> System.out.println(result),
				() -> System.out.println("I don't have a name"));

In this case as well, the result is as follows

//to name"I have a name"When assigned to
I have a name

//If name is null
I don't have a name

In the first place, there are more ways to write a normal if statement ... Or something like that, if you put it aside and explain what you are doing above Optional.ofNullable(name) If the value of the argument is not null in the part of, the Optinal that stores the value is returned. As the name suggests, it is a method that allows null, and if it is null, it returns an empty Optional generated by the optional empty method. Compare this empty Optional, == new with the empty Optional ... I don't do anything like that.

That's where ifPresent, a method of the Optional class, comes up. So, I thought about using ifPresent this time, but I saw many articles saying that ifPresent should not be used in various places. Apparently, if the Optional that I got first was empty, the main reason was that the else processing was troublesome. So this time, I used *** ifPresentOrElse *** newly added from java9. *** ifPresentOrElse (result-> System.out.println (result), ()-> System.out.println ("no name")) *** It is a method that is easy to understand at a glance and can sort the processing depending on whether the Optional obtained first is empty.

With this kind of feeling, process the value that may be null without approaching it with if null etc. I think it has the advantage of not only being able to write neatly, but also preventing forgetting to check for nulls.

So far this time, what is Optional?

Recommended Posts

Basic usage of java Optional Part 1
Java review ③ (Basic usage of arrays / reference type)
Super basic usage of Eclipse
[Java] Basic summary of Java not covered by Progate ~ Part 1 ~
[Java] Basic summary of Java not covered by Progate ~ Part 2 · List ~
Basic processing flow of java Stream
[Basic knowledge of Java] Scope of variables
Basic structure of Java source code
Profiling with Java Visual VM ~ Basic usage ~
Examine the memory usage of Java elements
[Java] Personal summary of conditional statements (basic)
Basic knowledge of Java development Note writing
Basic usage of enums and code examples
[Basic knowledge of Java] About type conversion
Java basic grammar
Java Optional type
Studying Java 8 (Optional)
java practice part 1
Java basic knowledge 1
[Java] Basic structure
[Java] [Basic] Glossary
Java basic grammar
Java basic grammar
[Java] Overview of Java
Java exercises [Basic]
[Java] Optional memorandum
Java 9 Optional :: stream
[Java] Personal summary of classes and methods (basic)
[Java] use Optional instead of if (hoge! = null) {...}
[Java] The confusing part of String and StringBuilder
Java engineers now compare to learn the basic grammar of Ruby Part 1 (Basic, Variables)
Expired collection of java
Java engineers now compare to learn the basic grammar of Ruby Part 2 (classes, methods)
Predicted Features of Java
Introduction of Docker --Part 1--
A quick review of Java learned in class part4
NIO.2 review of java
java basic knowledge memo
[Java] Data type ①-Basic type
Minimal usage of Mockito
study of dbpedia part 25
Regarding Java variable usage
Java basic date manipulation
View Java Optional Javadoc
Java basic naming conventions
Basic format of Dockefile
History of Java annotation
java (merits of polymorphism)
Java learning memo (basic)
Introduction to Java for beginners Basic knowledge of Java language ①
Studying Java ~ Part 8 ~ Cast
A quick review of Java learned in class part3
A quick review of Java learned in class part2
NIO review of java
[Java] Basic method notes
[Java] Three features of Java
Summary of Java support 2018
Summarize the additional elements of the Optional class in Java 9
Replace only part of the URL host with java
Java basic data types
Basic Java OOps concepts