Java 8 -- Optional, javaoptional

Source: Internet
Author: User

Java 8 -- Optional, javaoptional

This article mainly introducesJava 8OfOptionalEasy to use

Address
1234567 @ Data @ AllArgsConstructor @ NoArgsConstructorpublic class Address {private String province; private String city ;}
Of + ofNullable

Relevant code is hosted in java8_demo

123456789101112131415161718 @ Test (expected = NoSuchElementException. class) public void emptyTest () {// declare an empty Optional object Optional <Address> nameOptional = Optional. empty (); // java. util. noSuchElementException: No value presentnameOptional. get () ;}@ Test (expected = NullPointerException. class) public void ofNullTest () {// create the Optional object Optional Based on the instance. of (new Address ("Guangdong", "Zhongshan"); // Optional. ofNullable (null) returns Optional. empty () assertEquals (Optional. empty (), Optional. ofNullable (null); // java. lang. nullPointerExceptionOptional. of (null );}

 

Map

1234567891011 @ Testpublic void mapTest () {String province = "Guangdong"; String city = "Zhongshan"; Optional <Address> addressOptional = Optional. of (new Address (province, city); // mapper: Address-> String // Optional <Address>-> Optional <String> stringOptional = addressOptional. map (Address: getCity); assertTrue (stringOptional. isPresent (); assertEquals (city, stringOptional. get ());}
FlatMap

123456789101112 @ Testpublic void flatmapTest () {String province = "Guangdong"; String city = "Zhongshan"; Optional <Address> addressOptional = Optional. of (new Address (province, city); // call Optional when mapper is T-> Optional <U>. map, which generates Optional <U> Optional <String> optionalOptional = addressOptional. map (address-> Optional. ofNullable (address. getCity (); // call Optional when mapper is T-> Optional <U>. map, which generates Optional <U> and is flattened by Optional <String> stringOptional = addressOptional. flatMap (address-> Optional. ofNullable (address. getCity (); assertTrue (stringOptional. isPresent (); assertEquals (city, stringOptional. get ());}
Unreference
1234567891011121314151617181920212223242526 @ Test (expected = UnsupportedOperationException. class) public void dereferenceTest () {// get: Simplest + least secure Address address = addressOptional. get (); assertNotNull (address); address = null; Optional <Address> emptyAddressOptional = Optional. ofNullable (address); String defaultValue = "Unknown"; // orElse: Set the default Address elseAddress = emptyAddressOptional. orElse (new Address (defaultValue, defaultValue); assertEquals (defaultValue, elseAddress. getProvince (); assertEquals (defaultValue, elseAddress. getCity (); // orElseGet: delayed call version of orElse Address elseGetAddress = emptyAddressOptional. orElseGet (Address: new); assertNull (elseGetAddress. getProvince (); assertNull (elseGetAddress. getCity (); // ifPresent: If a value exists, run consumer. Otherwise, no operation is performed on emptyAddressOptional. ifPresent (System. out: println); // orElseThrow: if it does not exist, an emptyAddressOptional exception is thrown. orElseThrow (UnsupportedOperationException: new );}
Filter
1234 @ Testpublic void filterTest () {assertTrue (addressOptional. filter (address-> address. getCity (). contains ("Z"). isPresent ());}
Basic Type + 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.