Less nonsense:the Java.util.Optional class can significantly reduce null judgments and NullPointerException exceptions.
1. Optional.of
Optional.of (null); //
2. optional.ofnullable
Optional.ofnullable (null); // no exception reported
3. Optional.get
Optional.ofnullable (null). get (); // report NULL pointer exception
4. Optional.orelse
String test = optional.ofnullable (null// test = "Hello,world" )
5. Optional.orelseget
Optional.ofnullable ("123"). Orelseget ((), "Never Carry out my"); // lazy calculation.
5. Optional.ispresent
This is no use, you have to use and XX! = NULL What is the difference?
6. Optional.map
Give you a list of students, how do you get back to the student's name list?
Optional.ofnullable (Stus). Map (Student::getname). OrElse (Collection.emptylist ());
7. Optional.flatmap
Give you a list of students, you are responsible for each student's preferences collected, and then organized into another list, how to do?
Optional.ofnullable (Stus). FlatMap (Stu-optional.ofnullable (Stu.getfavoutites ())). OrElse ( Collection.emptylist ());
A word about the java.util.Optional