If you look at the JavaDoc for a detailed explanation, you're talking about what you're writing this for, so a brief explanation of the Optional class will be useful when dealing with data that you don't know if it's null or not.
Actually, it's been a while since I moved the development environment to Java8, but in fact, I didn't like the way I wrote it so far. However, I was surprised to use it and regretted not using it.
I would like to write about such an Optional class.
https://docs.oracle.com/javase/jp/8/docs/api/java/util/Optional.html
Optional.ofNullable("aaa")
Optional.of("aaa")
Instead of creating a new, use ʻof ʻof Nullable
to create the value.
The difference between the two is the name, the nullable one uses ʻofNullable and the non-null one uses ʻof
.
Since ʻof is value-checked with ʻObjects.requireNonNull
, passing null will result in a NullPointerException
.
If the acquisition method also needs to be non-null, the acquisition method differs depending on whether it is nullable.
First from the nonnull case
get()
orElseThrow(Supplier<? extends X> exceptionSupplier)
If get
is null, a NullPointerException
will occur.
ʻOrElseThrow` can control Exception when it is null with an argument.
Just use the method you want to control the Exception.
Next is the nullable case.
orElse(T other)
orElseGet(Supplier<? extends T> other)
ʻOr Else` is used by passing the value when the value of Optional is null as an argument (image passing the default value). The argument is used as is, so it's okay to put null here.
If the value of Optional is null, ʻorElseGet` calls get of Supplier passed by other and the value is returned. The value of Optional can be nullable, but please note that the Supplier must be non-null.
The Optional class implements map`` flatmap
to convert values and filter
to filter, so if you have a value, you can easily implement what you want to convert or filter.
If there is no value, it is not processed in particular, so it is usually convenient.
The difference between map
and flatmap
is difficult to describe in words, so I will omit it.
Basically, I think that only map
will do something.
isPresent()
ifPresent(Consumer<? super T> consumer)
ʻIsPresent returns true if the data exists, false if it does exist. Since there is ʻor Else
, there is no need to check it when retrieving the value, but without it, that is a problem.
ʻIfPresent` is a handy child that handles what you should do if the data exists by passing it in a lambda expression. Since it is a void type, it can only be processed simply, but it is troublesome to take it out and process it.
I will put the source I made for checking the operation to make this article this time. It's not a big deal, but I hope it helps.
package javasample;
import java.util.Optional;
public class Main {
public static void main(String[] args) {
Optional<String> str = Optional.ofNullable("aaa");
Optional<String> str2 = Optional.ofNullable("");
Optional<String> str3 = Optional.ofNullable(null);
Optional<String> str4 = Optional.of("aaa");
Optional<String> str5 = Optional.of("");
// Optional<String> str6 = Optional.of(null);
Optional<Text> text = Optional.ofNullable(null);
Optional<Text> text2 = Optional.ofNullable(new Text());
System.out.println(str.get());
System.out.println(str2.get());
System.out.println(str3.orElse(null));
System.out.println("");
System.out.println(str4.get());
System.out.println(str5.get());
// System.out.println(str6.get());
System.out.println("");
System.out.println(text.orElse(null));
System.out.println(text.map(Text::getText).orElse("ccc"));
System.out.println(text.flatMap(text3 -> Optional.of(text3.text)).orElse(null));
System.out.println(text2.get());
System.out.println(text2.map(Text::getText2).get());
System.out.println(text2.flatMap(text3 -> Optional.of(text3.text)).orElse(null));
System.out.println("");
str.ifPresent(text3 -> System.out.println(text3 + text3));
str2.ifPresent(text3 -> System.out.println(text3 + text3));
str3.ifPresent(text3 -> System.out.println(text3 + text3));
}
public static class Text {
private String text = "aaa";
private String text2 = "bbb";
public String getText() {
return text;
}
public String getText2() {
return text2;
}
}
}
Recommended Posts