Some of the content comes from the following Blogs:
https://www.cnblogs.com/1ning/p/9140800.html
1 Overview
For Java developers, NullPointException is a very common exception. In order to avoid this exception, the usual way is to judge the if condition. However, if the code is full of a large number of null judgments, the program will no longer be elegant.
Therefore, JDK1.8 introduces a new Optional class to handle variables that may be null, which not only reduces NullPointException, but also improves the beauty of the code. But first of all, we need to make it clear that it is not a substitute for null keyword, but provides a more elegant implementation for null determination, so as to avoid NullPointException.
2 get
2.1 empty() method
Meaning: used to create an empty Optional object.
The code is as follows:
Optional<String> name = Optional.empty();
2.2 method
Meaning: used to wrap a non null object. If the object is null, NullPointException will be thrown.
The code is as follows:
Optional<String> name = Optional.of(str);
2.3 ofNullable() method
Meaning: used to wrap a nullable object. If the object is null, an empty Optional object will be created.
The code is as follows:
Optional<String> name = Optional.ofNullable(str);
3 common methods
3.1 isPresent() method
Meaning: used to determine whether the Optional object is empty. If it is empty, it returns true. If it is not empty, it returns false.
The code is as follows:
Optional<String> name = Optional.ofNullable(null); System.out.println(name.isPresent());// true
3.2 ifPresent() method
Meaning: if the Optional object is not empty, the method call will be executed; if it is empty, nothing will be done.
The code is as follows:
Optional<String> name = Optional.ofNullable("name"); name.ifPresent(System.out::println);// name
3.3 get() method
Meaning: if the Optional object is not empty, it returns a value. If it is empty, it throws a NoSuchElementException.
The code is as follows:
Optional<String> name = Optional.ofNullable("name"); System.out.println(name.get());// name
3.4 orElse() method
Meaning: if the Optional object is not empty, the value is returned; if it is empty, the specified value is returned.
The code is as follows:
Optional<String> name = Optional.ofNullable(null); System.out.println(name.orElse("no name"));// no name
3.5 orElseGet() method
Meaning: if the Optional object is not empty, the value will be returned; if it is empty, the value generated by the incoming Supplier functional interface will be returned.
The code is as follows:
Optional<String> name = Optional.ofNullable(null); System.out.println(name.orElseGet(() -> "no name"));// no name
3.6 orElseThrow() method
Meaning: if the Optional object is not empty, it returns a value. If it is empty, it returns an exception generated by the incoming Supplier functional interface.
The code is as follows:
Optional<String> name = Optional.ofNullable(null); System.out.println(name.orElseThrow(() -> new NullPointerException()));// java.lang.NullPointerException
3.7 filter() method
Meaning: judge whether the Optional value meets the conditions specified by the filter. If so, the original Optional object will be returned. If not, an empty Optional object will be created and returned.
The code is as follows:
Optional<String> name = Optional.ofNullable("name").filter(e -> e != null); System.out.println(name);// Optional[name]
3.8 map() method
Meaning: if Optional is not empty, the Function function interface with the return value of any type will be executed and the return value will be wrapped as an Optional object. If Optional is empty, an empty Optional object will be created and returned.
The code is as follows:
Optional<Integer> length = Optional.ofNullable("name").map(String::length); System.out.println(length);// Optional[4]
3.9 flatMap() method
Meaning: if option is not empty, execute the Function function interface with the returned value of option type and get the returned option object. If option is empty, create an empty option object and return it.
The code is as follows:
User user = null; user = Optional.ofNullable(user).flatMap(Optional::ofNullable).orElse(new User()); System.out.println(user);// test.User@768debd
4 precautions
4.1 difference between orelse() method and orElseGet() method
When the Optional object called by these two methods is not empty, they return the original object. When the Optional object is null, the orElse() method returns the specified value, and the orElseGet() method returns the return value of the interface call.
In addition, if the Optional object is not empty, the orElse() method will also be executed, and the orElseGet() method will not be executed.
For example, the orElse() method returns a new object, and the orElseGet() method calls the interface to return a new object. If the Optional object is not empty, the orElse() method will perform initialization, and the orElseGet() method will not perform initialization.
4.2 difference between map () method and flatMap() method
When the Optional object called by these two methods is empty, an empty object will be created and returned. If the Optional object is not empty, the map() method will wrap the return value obtained by the execution interface as an Optional object, and the flatMap() method will directly return the return value of the Optional type obtained by the execution interface.