Summary of the use of JAVA8 optional class

Source: Internet
Author: User
The Javadoc description for the optional class is as follows:

This is a container object that can be null. If the value exists, the Ispresent () method returns True, and the call to the Get () method returns the object.

Of
Creates a optional for a value that is not NULL.
The method creates the optional class from the factory method. It should be noted that the parameters passed in when the object was created cannot be null. If the incoming argument is null, the NullPointerException is thrown.

Invokes the factory method to create
the Optional instance optional<string> name = Optional.of ("Sanaulla");
The passed-in parameter is null and throws a nullpointerexception.
optional<string> somenull = Optional.of (null);
Ofnullable:
Creates a optional for the specified value, and returns an empty optional if the specified value is null.
Ofnullable is similar to the of method, the only difference is that you can accept a condition where the parameter is null. Examples are as follows:

The following creates a Optional instance that does not contain any values
//For example, the value is ' null '
Optional empty = optional.ofnullable (null);
Ifpresent:
Call consumer If the optional instance has a value, otherwise no processing
If the optional instance has a value, calling Ifpresent () can accept an interface segment or lambda expression. Code similar to the following:
The Ifpresent method accepts a lambda expression as an argument. The
//LAMBDA expression handles the optional value invocation consumer.
name.ifpresent ((value)-> {
  System.out.println ("The length of the value is:" + value.length ());
});
OrElse:
If there is a value, it is returned, otherwise the specified other value is returned.
Returns the parameter passed by the OrElse method if the optional instance has a value. Examples are as follows:
Returns the value of the optional instance if the value is not the Null,orelse method.
//If NULL, returns the incoming message.
//output: There is no value present!
System.out.println (Empty.orelse ("There is no value present!"));
Output: Sanaulla
System.out.println (Name.orelse ("There is some value!"));
Orelseget
Orelseget is similar to the OrElse method, except for the resulting default value. The OrElse method takes the Passed-in string as the default value, and the Orelseget method can accept implementations of the supplier interface to generate default values. Examples are as follows:

Orelseget is similar to the OrElse method, except that OrElse pass in a default value,
//orelseget can accept a lambda expression to generate a default value.
//output: Default Value
System.out.println (Empty.orelseget ()-> "Default Value");
Output: Sanaulla
System.out.println (name.orelseget ()-> "Default Value");
Map: If there is a value, the call to the mapping function gets the return value. If the return value is not NULL, the optional that contains the mapping return value is created as the map method return value, otherwise an empty optional is returned.
Examples of the map methods are as follows:

The map method executes the passed-in lambda expression parameter to modify the value of the optional instance.
//Creates a new optional instance for the return value of the lambda expression as the return value of the map method.
optional<string> uppername = Name.map ((value)-> value.touppercase ());
System.out.println (Uppername.orelse ("No value Found"));
Flatmap:
If there is a value, performing the mapping function for it returns the optional type return value, otherwise an empty optional is returned. Flatmap is similar to the map (funtion) method, except that the mapper return value in Flatmap must be optional. At the end of the call, Flatmap does not use optional encapsulation of the result.
The Flatmap method is similar to the map method, except that the return value of the mapping function is different. The mapping function return value of the map method can be any type T, and the mapping function of the Flatmap method must be optional.
Refer to the Map function, using the example of Flatmap rewrite as follows:
Flatmap is very similar to a map (Function), except for the return type of the lambda expression of the incoming method.
the lambda expression return value in the//map method can be any type and will be wrapped as optional before the map function returns. 
//But the lambda expression return value in the Flatmap method must be an OPTIONL instance. 
uppername = Name.flatmap ((value)-> Optional.of (Value.touppercase ()));
System.out.println (Uppername.orelse ("No value Found"));//Output Sanaulla
Filter
Returns an empty optional if there is a value and satisfies the assertion condition to return the optional containing the value.
The following example describes both conditions that meet the qualification and do not meet:
The filter method checks whether the given option value satisfies certain conditions.
//If satisfied, return the same option instance, otherwise return an empty optional.
optional<string> longname = Name.filter ((value)-> value.length () > 6);
System.out.println (Longname.orelse ("The name is less than 6 characters"))/output Sanaulla
Another example is that the optional value does not meet the criteria specified by the filter.
optional<string> anothername = Optional.of ("Sana");
optional<string> shortname = Anothername.filter ((value)-> value.length () > 6);
Output: Name length is less than 6 characters
System.out.println (Shortname.orelse ("The name is less than 6 characters"));
Reprint Address: http://www.importnew.com/6675.html

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.