一:簡介
方法引用分為三種,方法引用通過一對雙冒號:: 來表示,方法引用是一種函數式接口的另一種書寫方式
- 靜態方法引用,通過類名::靜態方法名, 如 Integer::parseInt
- 實例方法引用,通過實例對象::實例方法,如 str::substring
- 構造方法引用,通過類名::new, 如 User::new
二:方法引用
1
2
3
4
5
|
public final class Integer { public static int parseInt(String s) throws NumberFormatException { return parseInt(s, 10 ); } } |
通過方法引用,可以將方法的引用賦值給一個變量,通過賦值給Function,說明方法引用也是一種函數式接口的書寫方式,Lambda表達式也是一種函數式接口,Lambda表達式一般用于自己提供方法體,而方法引用一般直接引用現成的方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public class User { private String username; private Integer age; public User() { } public User(String username, Integer age) { this .username = username; this .age = age; } @Override public String toString() { return "User{" + "username='" + username + '\ '' + ", age=" + age + '}' ; } // Getter&Setter } |
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
|
public static void main(String[] args) { // 使用雙冒號::來構造靜態函數引用 Function<String, Integer> fun = Integer::parseInt; Integer value = fun.apply( "123" ); System.out.println(value); // 使用雙冒號::來構造非靜態函數引用 String content = "Hello JDK8" ; Function<Integer, String> func = content::substring; String result = func.apply( 1 ); System.out.println(result); // 構造函數引用 BiFunction<String, Integer, User> biFunction = User:: new ; User user = biFunction.apply( "mengday" , 28 ); System.out.println(user.toString()); // 函數引用也是一種函數式接口,所以也可以將函數引用作為方法的參數 sayHello(String::toUpperCase, "hello" ); } // 方法有兩個參數,一個是 private static void sayHello(Function<String, String> func, String parameter){ String result = func.apply(parameter); System.out.println(result); } |
三:Optional 可選值
在Google Guava 中就有Optional,在Swift語言中也有這樣類似的語法,在Swift中將可選值作為一種數據類型,地位和基本類型平齊平做,地位非常高。
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
package java.util; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; /** * @since 1.8 */ public final class Optional<T> { private static final Optional<?> EMPTY = new Optional<>(); private final T value; private Optional() { this .value = null ; } // 返回一個空的 Optional實例 public static <T> Optional<T> empty() { @SuppressWarnings ( "unchecked" ) Optional<T> t = (Optional<T>) EMPTY; return t; } private Optional(T value) { this .value = Objects.requireNonNull(value); } // 返回具有 Optional的當前非空值的Optional public static <T> Optional<T> of(T value) { return new Optional<>(value); } // 返回一個 Optional指定值的Optional,如果非空,則返回一個空的 Optional public static <T> Optional<T> ofNullable(T value) { return value == null ? empty() : of(value); } // 如果Optional中有一個值,返回值,否則拋出 NoSuchElementException 。 public T get() { if (value == null ) { throw new NoSuchElementException( "No value present" ); } return value; } // 返回true如果存在值,否則為 false public boolean isPresent() { return value != null ; } // 如果存在值,則使用該值調用指定的消費者,否則不執行任何操作。 public void ifPresent(Consumer<? super T> consumer) { if (value != null ) consumer.accept(value); } // 如果一個值存在,并且該值給定的謂詞相匹配時,返回一個 Optional描述的值,否則返回一個空的 Optional public Optional<T> filter(Predicate<? super T> predicate) { Objects.requireNonNull(predicate); if (!isPresent()) return this ; else return predicate.test(value) ? this : empty(); } // 如果存在一個值,則應用提供的映射函數,如果結果不為空,則返回一個 Optional結果的 Optional 。 public <U> Optional<U> map(Function<? super T, ? extends U> mapper) { Objects.requireNonNull(mapper); if (!isPresent()) return empty(); else { return Optional.ofNullable(mapper.apply(value)); } } // 如果一個值存在,應用提供的 Optional映射函數給它,返回該結果,否則返回一個空的 Optional 。 public <U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper) { Objects.requireNonNull(mapper); if (!isPresent()) return empty(); else { return Objects.requireNonNull(mapper.apply(value)); } } // 如果值存在,就返回值,不存在就返回指定的其他值 public T orElse(T other) { return value != null ? value : other; } public T orElseGet(Supplier<? extends T> other) { return value != null ? value : other.get(); } public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X { if (value != null ) { return value; } else { throw exceptionSupplier.get(); } } } |
關于of方法,現在好像很流行,就是提供一個static方法,方法名稱叫of,方法的返回值返回當前類,并且把構造函數設置為私有private,用靜態of方法來代替構造函數。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public class User { private String username; private Integer age; private User() { } public static User of() { return new User(); } private User(String username, Integer age) { this .username = username; this .age = age; } public static User of(String username, Integer age) { return new User(username, age); } } |
Main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public static void main(String[] args) { // Optional類已經成為Java 8類庫的一部分,在Guava中早就有了,可能Oracle是直接拿來使用了 // Optional用來解決空指針異常,使代碼更加嚴謹,防止因為空指針NullPointerException對代碼造成影響 String msg = "hello" ; Optional<String> optional = Optional.of(msg); // 判斷是否有值,不為空 boolean present = optional.isPresent(); // 如果有值,則返回值,如果等于空則拋異常 String value = optional.get(); // 如果為空,返回else指定的值 String hi = optional.orElse( "hi" ); // 如果值不為空,就執行Lambda表達式 optional.ifPresent(opt -> System.out.println(opt)); } |
到此這篇關于JDK1.8新特性之方法引用 ::和Optional的文章就介紹到這了,更多相關JDK1.8新特性Optional內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/vbirdbest/article/details/80207673