首页> Java> java教程> 正文

Java 8——Optional

巴扎黑
发布: 2017-06-23 15:48:29
原创
1566 人浏览过

本文主要介绍Java 8Optional的简单使用

Address

1
2
3
4
5
6
7
@Data
@AllArgsConstructor
@NoArgsConstructor
publicclassAddress {
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)
publicvoidemptyTest() {
// 声明一个空的Optional对象
Optional
nameOptional = Optional.empty();
// java.util.NoSuchElementException: No value present
nameOptional.get();
}
@Test(expected = NullPointerException.class)
publicvoidofNullTest() {
// 依据实例创建Optional对象
Optional.of( new Address("Guangdong","Zhongshan"));
// Optional.ofNullable(null)返回Optional.empty()
assertEquals(Optional.empty(), Optional.ofNullable( null));
// java.lang.NullPointerException
Optional.of( null);
}

map

  1. Stream.map的定义: Stream map(Function mapper)

  2. Optional.map的定义: Optional map(Function mapper)

  3. 上面两者定义非常类似,Optional可看成最多包含一个元素Stream

java8-optional-map

1
2
3
4
5
6
7
8
9
10
11
@Test
publicvoidmapTest() {
String province = "Guangdong";
String city = "Zhongshan";
Optional
addressOptional = Optional.of(new Address(province, city));
// mapper : Address -> String
// Optional
-> Optional
Optional stringOptional = addressOptional.map(Address::getCity);
assertTrue(stringOptional.isPresent());
assertEquals(city, stringOptional.get());
}

flatMap

  1. Stream.flatMap的定义: Stream flatMap(Function> mapper)

  2. Optional.flatMap的定义: Optional flatMap(Function> mapper)

java8-optional-flatmap

1
2
3
4
5
6
7
8
9
10
11
12
@Test
publicvoidflatmapTest() {
String province = "Guangdong";
String city = "Zhongshan";
Optional
addressOptional = Optional.of(new Address(province, city));
// 对于 mapper 为 T -> Optional时,调用Optional.map,生成的是Optional >
Optional > optionalOptional = addressOptional.map(address -> Optional.ofNullable(address.getCity()));
// 对于 mapper 为 T -> Optional时,调用Optional.map,生成的是Optional,被扁平化
Optional 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)
publicvoiddereferenceTest() {
// get:最简单 + 最不安全
Address address = addressOptional.get();
assertNotNull(address);
address = null;
Optional
emptyAddressOptional = Optional.ofNullable(address);
String defaultValue = "Unknown";
// orElse:设置默认值
Address elseAddress = emptyAddressOptional.orElse( new Address(defaultValue, defaultValue));
assertEquals(defaultValue, elseAddress.getProvince());
assertEquals(defaultValue, elseAddress.getCity());
// orElseGet:orElse的延迟调用版本
Address elseGetAddress = emptyAddressOptional.orElseGet(Address:: new);
assertNull(elseGetAddress.getProvince());
assertNull(elseGetAddress.getCity());
// ifPresent:存在值则运行consumer,否则不进行任何操作
emptyAddressOptional.ifPresent(System.out::println);
// orElseThrow:不存在时,抛出异常
emptyAddressOptional.orElseThrow(UnsupportedOperationException:: new);
}

filter

1
2
3
4
@Test
publicvoidfilterTest() {
assertTrue(addressOptional.filter(address -> address.getCity().contains( "Z")).isPresent());
}

基础类型 + Optional

  1. OptionalIntOptionalLongOptionalDouble

  2. 最多只有一个元素,并没有像StreamInt那样相对于Stream有性能优势

  3. 不支持mapflatmapfilter

以上是Java 8——Optional的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!