Home > Java > javaTutorial > Java 8——Optional

Java 8——Optional

巴扎黑
Release: 2017-06-23 15:48:29
Original
1670 people have browsed it

This article mainly introduces the simple use of Optional of Java 8

Address

##of + ofNullable
1
2
3
4
5
6
7
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Address {
##private String province;
private String city;
}

Related code hosting In java8_demo

##1@Test(expected = NoSuchElementException.class)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
##public
void
emptyTest() { //Declare an empty Optional object
Optional
nameOptional = Optional.empty();
// java.util.NoSuchElementException: No value present
nameOptional.get();}
@Test(expected = NullPointerException.class)
public
void
ofNullTest() {// Create an Optional object based on the instance
Optional.of(new Address("Guangdong",
"Zhongshan"));
// Optional.ofNullable(null) returns Optional.empty()
assertEquals(Optional.empty(), Optional.ofNullable(null));
// java.lang.NullPointerException
Optional.of(null);
}

map

  1. Definition of Stream.map: <r> Stream<r> map(Function super T, ? extends R> mapper)</r></r>

  2. ##Definition of Optional.map: Optional map(Function< ;? super T, ? extends U> mapper)

  3. The above two definitions are very similar,

    Optional can be regarded as containing at most one element Stream

Java 8——Optional

##1flatMap
2
3
4
5
6
7
8
9
10
11
@Test
public void mapTest() {##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());
}

##Definition of Stream.flatMap
    :
  1. < ;R> Stream flatMap(Function super T, ? extends Stream extends R>> mapper)

    ##Optional.flatMap## Definition of #:

    Optional flatMap(Function super T, Optional> mapper)

##1Java 8——Optional

23
4
5
6
7
8
9
10
11
12
@Test
public
void flatmapTest() {
String province = "Guangdong";String city = "Zhongshan";
Optional
addressOptional = Optional.of(
new Address(province, city));
// When mapper is T -> Optional, Calling Optional.map generates Optional>
##Optional> optionalOptional = addressOptional.map(address -> Optional.ofNullable(address.getCity ()));
// When mapper is T -> Optional, when Optional.map is called, Optional is generated and is flattened
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)
public void dereferenceTest() {
// 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
public void filterTest() {
assertTrue(addressOptional.filter(address -> address.getCity().contains("Z")).isPresent());
}

基础类型 + Optional

  1. OptionalIntOptionalLongOptionalDouble

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

  3. 不支持 map 、 flatmap 、 filter

The above is the detailed content of Java 8——Optional. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template