Java8: (Optional)

Source: Internet
Author: User
Tags getmessage

1.Optional:

returnUser.map (U-u.getorders ()). OrElse (Collections.emptylist () )//It avoids our previous practices like Java 8.if(User.ispresent ()) {returnuser.get (). getorders ();}Else{  returncollections.emptylist ();} Map can be infinitely cascaded, such as deep, to get the uppercase form of the user namereturnUser.map (U-u.getusername ()). Map (name-name.touppercase ()). OrElse (NULL)            ;//If this is the case before, the following is the wording:User User = ....if(User! =NULL) {String name=User.getusername (); if(Name! =NULL){    returnname.touppercase (); }Else{    return NULL; }}Else{  return NULL;} try not to call the Optional.get () method directly when using Optional, Optional.ispresent ()
should be considered as a private method and should depend on other methods such as Optional.orelse (), Optional.orelseget (), Optional.map ().

Public Static voidMain (string[] args) {//Create a optional instance, or you can return it by means of a method. optional<string> name = Optional.of ("Sanaulla"); //Create an optional instance with no value, for example, a value of ' null 'Optional empty = optional.ofnullable (NULL); //The Ispresent method is used to check whether the optional instance has a value. if(Name.ispresent ()) {//call Get () to return the optional value. System.out.println (Name.get ()); } Try { //call Get () on the optional instance to throw the nosuchelementexception. System.out.println (Empty.get ()); } Catch(Nosuchelementexception ex) {System.out.println (Ex.getmessage ()); } //The Ifpresent method accepts the lambda expression parameter. //If the optional value is not empty, the lambda expression processes and executes the action on it. Name.ifpresent (value){System.out.println ("The length of the value is:" +value.length ()); }); //if the value OrElse method returns an optional instance, the incoming error message is returned. System.out.println (Empty.orelse ("There is no value present!")); System.out.println (Name.orelse ("There is some value!")); //Orelseget is similar to OrElse, except that the default value is passed in. //Orelseget accepts a lambda expression to generate a default value. System.out.println (Empty.orelseget (), "Default Value")); System.out.println (Name.orelseget ()"Default Value")); Try { //Orelsethrow is similar to the OrElse method, except that the return value is the difference. //Orelsethrow thrown by an incoming lambda expression/method generates an exception. Empty.orelsethrow (valueabsentexception::New); } Catch(Throwable ex) {System.out.println (Ex.getmessage ()); } //The Map method modifies the default value of the Optonal instance by passing in the lambda expression. //the lambda expression return value is wrapped as an optional instance. optional<string> uppername = Name.map ((value)value.touppercase ()); System.out.println (Uppername.orelse ("No value found")); //Flatmap is very similar to map (Funtion), except for the return value of a lambda expression. //the lambda expression return value of the map method can be any type, but the return value is wrapped as a optional instance. //However, the lambda return value of the Flatmap method is always optional type. Uppername = Name.flatmap ((value)Optional.of (Value.touppercase ())); System.out.println (Uppername.orelse ("No value found")); //The filter method checks whether the Optiona value satisfies the given condition. //returns an empty optional if the return optional instance value is satisfied. optional<string> longname = Name.filter ((value) Value.length () > 6); System.out.println (Longname.orelse ("The name is less than 6 characters")); //Another example, the optional value does not meet the given criteria. optional<string> anothername = Optional.of ("Sana"); Optional<String> shortname = Anothername.filter ((value), Value.length () > 6); System.out.println (Shortname.orelse ("The name is less than 6 characters")); }

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.