Java 8 Optional Class

Source: Internet
Author: User

Java 8 Optional Class

New features in Java 8

The Optional class is a nullable container object. If the value is present, the Ispresent () method returns True, and the call to the Get () method returns the object.

Optional is a container: it can hold the value of type T, or just save null. Optional provides a number of useful methods so that we do not have to explicitly perform null-value detection.

The introduction of the Optional class is a good solution to null pointer exceptions.

class declaration

The following is a declaration of a java.util.optional<t> class:

public final class Optional<T> extends Object Class method
Serial Number Method & Description
1 Static <T> optional<t> Empty ()

Returns an empty Optional instance.

2 Boolean equals (Object obj)

Determines whether other objects are equal to Optional.

3 optional<t> filter (predicate<? Super <T> predicate)

If the value exists and the value matches the given predicate, a optional is returned to describe the value, otherwise an empty optional is returned.

4 <U> optional<u> flatMap (function<? Super T,optional<u>> Mapper)

If the value exists, returns a value based on the mapping method contained by the optional, otherwise returns an empty optional

5 T get ()

If you include this value in this optional, return the value, or throw an exception: Nosuchelementexception

6 int Hashcode ()

Returns a hash code that has a value that returns 0 if the value does not exist.

7 void Ifpresent (consumer<? Super T> Consumer)

If the value exists then use that value to call consumer, otherwise do nothing.

8 Boolean isPresent ()

If the value is present, the method returns True, otherwise false is returned.

9 <U>Optional<U> map (function<? Super T,? Extends u> mapper)

If the value is present, the provided mapping method, if returned non-null, returns a optional that describes the result.

10 Static <T> optional<t> of (T value)

Returns a optional that specifies a non-null value.

11 Static <T> optional<t> ofnullable (T value)

If non-null, returns the specified value that Optional describes, otherwise returns an empty Optional.

12 T OrElse (t other)

If the value is present, return the value, otherwise return to other.

13 T orelseget (supplier<? extends t> other)

If the value is present, return the value, otherwise trigger other and return the result of the other call.

14 <x extends throwable> T orelsethrow (supplier<? extends x> Exceptionsupplier)

If the value exists, returns the contained value, otherwise throws an exception inherited by Supplier

15 String toString ()

Returns a optional non-empty string that is used to debug

Note: These methods are inherited from the Java.lang.Object class.

Optional instances

We can better understand the use of the Optional class by using the following examples:

Java8tester.java fileImport Java.Util.Optional;Public Class Java8tester { Public Static void Main(String Args[]){ Java8tester Java8tester=New Java8tester();Integer Value1=Null;Integer value2=New Integer(10);//Optional.ofnullable-Allow pass as null parameter Optional<Integer>A=Optional.Ofnullable(Value1);//Optional.of-Throws an exception if the passed argument is null NullPointerException Optional<Integer>B=Optional.Of(value2);System.Out.println(Java8tester.Sum(A,B));} Public Integer Sum(Optional<Integer>A,Optional<Integer>B){ //Optional.ispresent-Determine if a value exists System.Out.println("The first parameter value exists:"+A.IsPresent());System.Out.println("The second value of the parameter exists:"+B.IsPresent());//Optional.orelse-Returns the value if it exists, otherwise returns the default value Integer Value1=A.OrElse(New Integer (0) Span class= "Hl-code" >; //optional.get-get value, value needs to exist integer value2 = b. Getreturn value1 + value2; } } /span>

Execute the above script and the output is:

Java8tester.  Java8tester the first parameter value exists:false The second parameter value exists:true      

Java 8 Optional Class

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.