首頁 >Java >java教程 >Java 8——可選

Java 8——可選

巴扎黑
巴扎黑原創
2017-06-23 15:48:291665瀏覽

本文主要介紹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#

 

map

  1. Stream.map的定義:<r> Stream<r> map(Function super T, ? extends R> mapper)</r></r>

  2. Optional.map的定義:<u> Optional<u> map(Function&lt ;? super T, ? extends U> mapper)</u></u>

  3. 上面兩者定義非常類似,Optional可看成最多包含一個元素的 Stream

Java 8——可選

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物件
Optional
nameOptional = Optional.empty();
// java.util.NoSuchElementException: No value present
nameOptional.get();
##}
#@Test(expected = NullPointerException.class)
#public void ofNullTest() {
// 依據實例建立Optional物件
Optional.of(
new Address("Guangdong", "Zhongshan"));
 
// Optional.ofNullable(null)回傳Optional.empty()
assertEquals(Optional.empty(), Optional.ofNullable(
null));
// java.lang.NullPointerException
Optional.of(
null);
}
8Optional stringOptional = addressOptional.map(Address::getCity);
1
1
2
3
4
5
6
7
9
1011#@Test
##public void mapTest
() {
#String province =
"Guangdong";
##String city =
"Zhongshan";
Optional
addressOptional = Optional.of(
new Address(province, city));
// mapper : Address -> String
// Optional
-> Optional

assertTrue(stringOptional.isPresent());

    assertEquals(city, stringOptional.get());
  1. assertEquals(city, stringOptional。 ##}

  2. flatMap
  3. #Stream.flatMap的定義:

    < ;R> Stream flatMap(Function super T, ? extends Stream extends R>> mapper)

Java 8——可選

Optional.flatMap"Guangdong";
##Optional.flatMap
##Optional.flatMap
的定義:
Optional flatMap(Function super T, Optional> mapper)
12#3##4##5#6
78
910
1112
@Test
public
void
flatmapTest##() {
String province =
String city = ###"Zhongshan";######### #Optional
addressOptional = Optional.of(###new Address(province, city));############// 對於mapper 為T -> Optional時,呼叫Optional.map,生成的是Optional>#########Optional> optionalOptional = addressOptional.map(address ->> optionalOptional = addressOptional.map(address -> Optional.ofNullable). ()));#########// 對於mapper 為T -> Optional時,呼叫Optional.map,產生的是Optional,被扁平化###### ####Optional stringOptional = addressOptional.flatMap(address -> Optional.ofNullable(address.getCity()));######assertTrue(stringOptional.isPresent());######assertTrue(stringOptional.isPresent());#### #assertEquals(city, stringOptional.get());######}###############

解引用

20emptyAddressOptional.ifPresent(System.out::println) ; 
1
2
3
##4
# #5
6
7
8
9
10
##11
12
13
14
15
16
17
18
# 19
2122
#23#24#25#26
@Test(expected = UnsupportedOperationException.class)
public
void
dereferenceTest
() {
## // 取得:最簡單+ 最不安全
位址address = 位址可選.get();
assertNotNull(address);
 
address =
null;
#可選; emptyAddressOptional =Optional.ofNullable(address);
 
String defaultValue =
"Unknown";
// orElse:設定預設值
##位址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:存在值則運行消費者,否則不進行任何操作

#// orElseThrow:不存在時,發送異常#emptyAddressOptional.orElseThrow(UnsupportedOperationException::
new);
##}
過濾器
##1
234##@測試
# #公共void
filterTest
() {

assertTrue(addressOptional.filter(address ->位址.getCity().contains(
    "Z")).isPresent());
  1. ##}基礎類型+ 可選

  2. ##OptionalIntOptionalLongOptionalDouble

################################### #最多###只有一個###元素,並沒有​​像###StreamInt###那樣相對於###Stream###有效能優勢###########不支援###地圖###、###平面地圖###、###過濾#############

以上是Java 8——可選的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn