本文主要介绍Java 8
的 Optional
的简单使用
Address
1 2 3 4 5 6 7
| @Data @AllArgsConstructor @NoArgsConstructor public class Address { private String province; private String city; }
|
of + ofNullable
相关代码托管在java8_demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| @Test(expected = NoSuchElementException.class) public void emptyTest() { Optional<Address> nameOptional = Optional.empty(); nameOptional.get(); }
@Test(expected = NullPointerException.class) public void ofNullTest() { Optional.of(new Address("Guangdong", "Zhongshan"));
assertEquals(Optional.empty(), Optional.ofNullable(null)); Optional.of(null); }
|
map
Stream.map
的定义:<R> Stream<R> map(Function<? super T, ? extends R> mapper)
Optional.map
的定义:<U> Optional<U> map(Function<? super T, ? extends U> mapper)
- 上面两者定义非常类似,
Optional
可看成最多包含一个元素
的 Stream
1 2 3 4 5 6 7 8 9 10 11
| @Test public void mapTest() { String province = "Guangdong"; String city = "Zhongshan"; Optional<Address> addressOptional = Optional.of(new Address(province, city)); Optional<String> stringOptional = addressOptional.map(Address::getCity); assertTrue(stringOptional.isPresent()); assertEquals(city, stringOptional.get()); }
|
flatMap
Stream.flatMap
的定义:<R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper)
Optional.flatMap
的定义:<U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper)
1 2 3 4 5 6 7 8 9 10 11 12
| @Test public void flatmapTest() { String province = "Guangdong"; String city = "Zhongshan"; Optional<Address> addressOptional = Optional.of(new Address(province, city)); Optional<Optional<String>> optionalOptional = addressOptional.map(address -> Optional.ofNullable(address.getCity())); Optional<String> stringOptional = addressOptional.flatMap(address -> Optional.ofNullable(address.getCity())); assertTrue(stringOptional.isPresent()); assertEquals(city, stringOptional.get()); }
|
解引用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| @Test(expected = UnsupportedOperationException.class) public void dereferenceTest() { Address address = addressOptional.get(); assertNotNull(address);
address = null; Optional<Address> emptyAddressOptional = Optional.ofNullable(address);
String defaultValue = "Unknown"; Address elseAddress = emptyAddressOptional.orElse(new Address(defaultValue, defaultValue)); assertEquals(defaultValue, elseAddress.getProvince()); assertEquals(defaultValue, elseAddress.getCity());
Address elseGetAddress = emptyAddressOptional.orElseGet(Address::new); assertNull(elseGetAddress.getProvince()); assertNull(elseGetAddress.getCity());
emptyAddressOptional.ifPresent(System.out::println);
emptyAddressOptional.orElseThrow(UnsupportedOperationException::new); }
|
filter
1 2 3 4
| @Test public void filterTest() { assertTrue(addressOptional.filter(address -> address.getCity().contains("Z")).isPresent()); }
|
基础类型 + Optional
OptionalInt
、OptionalLong
、OptionalDouble
- 最多
只有一个
元素,并没有像StreamInt
那样相对于 Stream<Integer>
有性能优势
- 不支持
map
、 flatmap
、 filter