"Java8" Optional

Source: Internet
Author: User

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
  1. Explicitly reminds you of the need to focus on NULL, is a literal constraint to the programmer.

  2. Standardizes Some of the usual explicit defensive tests and provides some inline operations

  3. 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
  1. http://www.javacodegeeks.com/2013/09/ deep-dive-into-optional-class-api-in-java-8.html
  2. What's the point of guava ' s optional class
  3. http:/ /www.nurkiewicz.com/2013/08/optional-in-java-8-cheat-sheet.html

"Java8" Optional

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.