OptionalOptional Brief introduction
In the literal sense, the meaning should be optional. At first I thought it was a default parameter usage like Python, and the result semantics was that a value might or might not be (null).
The sensation of name acquisition is not very intuitive. I don't think it's better to call nullable?
Python:
DEF fn (a= ' default_value '): print (a) pass
Optional method
Optional there is no public constructor method, only the static factory method:
optional<string> optional_2 = Optional.of (str); if str = = NULL, throws an error nullpointerexceptionoptional<string> optional = optional.ofnullable (str); if str = = NULL, returns an empty Optional<string>optional.<string>empty (); Returns an empty optional<string>string s = optional.get (); Gets the wrapped value optional.ifpresent ((value), System.out.println ("Hello")); If the value of optional is not NULL, the function expression optional.orelse ("Elsevalue") is executed; If the value of optional is null, then return "Elsevalue" Optional.orelseget ((), "Orelseget"); If the value of optional is not NULL, the execution result of the function expression Optional.orelsethrow (runtimeexception::new) is returned; If the value of optional is NOT NULL, an error Optional.filter ((value), value.length () = = 5) is thrown; Filter to get value optional.map (value) = { System.out.println ("Map:" + value) of length equal to 5; return value;}); O Ptional.flatmap (value), { System.out.println ("FLATMAP:" + value); return optional.ofnullable (value);});
The difference between map and FlatMap
Map (Mapper) and FlatMap (mapper) function basically the same, but the final return value is not the same. The Mapper in the map (Mapper) method can return any type, but only the optional type is returned inside the Flatmap (mapper) method.
If mapper returns a result that is not NULL, then map returns a optional, but FlatMap does not make any wrappers on result. A common example:
optional<string> Os;os.map ((value) Optional.of (value))//The type returned is Optional<optional<string>>os.flatmap ((value)->optional.of (value))// The return type is Optional<string>
optional benefits
-
Explicitly reminds you of the need to focus on NULL, is a literal constraint to the programmer.
-
Standardizes Some of the usual explicit defensive tests and provides some inline operations
-
Solving the notion that null can cause confusion, such as the key== in a map Null case, and Value==null case
subsection
But optional all of a sudden there are so many ways, what is the purpose of optional? And optional is also an object, so it can also be null, which can be good.
Therefore, there is a view that optional is more applicable to the place as a return value, which can give the user a good-natured reminder.
reference
- http://www.javacodegeeks.com/2013/09/ deep-dive-into-optional-class-api-in-java-8.html
- What's the point of guava ' s optional class
- http:/ /www.nurkiewicz.com/2013/08/optional-in-java-8-cheat-sheet.html
"Java8" Optional