一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 如何更好的使用Java8中方法引用詳解

如何更好的使用Java8中方法引用詳解

2020-12-30 11:33颯然Hang Java教程

在Java8中,我們可以直接通過方法引用來簡寫lambda表達式中已經存在的方法,這種特性就叫做方法引用(Method Reference)。下面這篇文章主要給大家介紹了關于如何更好的使用Java8中方法引用的相關資料,需要的朋友可以參考下。

前言

方法引用是用來直接訪問類或者實例的已經存在的方法或者構造方法。方法引用提供了一種引用而不執行方法的方式,它需要由兼容的函數式接口構成的目標類型上下文。計算時,方法引用會創建函數式接口的一個實例。

當Lambda表達式中只是執行一個方法調用時,不用Lambda表達式,直接通過方法引用的形式可讀性更高一些。方法引用是一種更簡潔易懂的Lambda表達式。

注意:方法引用是一個Lambda表達式,其中方法引用的操作符是雙冒號"::"。

Java8中,使用方法引用非常簡單,如String::isEmpty,但無法使用它否定的方法引用。本文內容即如何解決此問題使得我們能夠更加全面地使用方法引用。

首先看一個使用方法引用的例子:

java" id="highlighter_453917">
?
1
Stream.of("A", "", "B").filter(String::isEmpty).count()

上面代碼的輸出為1,即空字符串的數目。如果我們想要獲取非空字符串的數目,就不能直接使用方法引用了。

?
1
Stream.of("A", "", "B").filter(s -> !s.isEmpty()).count()

Java8中的Predicate,有predicate.negate()可以轉換為斷言的否定形式,但String::isEmpty卻無法這么做(String::isEmpty.negate()或者!String::isEmpty )。因為方法引用并不是一個lambda或者函數接口,它能夠被解析為一個或者多個函數接口。如,String::isEmpty至少可以被解析如下:

?
1
2
Predicate<String>
Function<String, Boolean>

為了解決上述的問題,我們可以通過某種機制顯式地將方法引用轉換為一個函數接口:

?
1
2
3
public static <T> Predicate<T> as(Predicate<T> predicate) {
 return predicate;
}

通過使用一個靜態方法,接受方法引用參數,返回一個函數接口,即可實現方法引用到函數接口的轉換。接著,我們就可以使用方法引用來實現上面例子中的獲取非空字符串的數目。

?
1
Stream.of("A", "", "B").filter(as(String::isEmpty).negate()).count();

進一步還能使用各種組合的Predicate。

?
1
.filter(as(String::isEmpty).negate().and("A"::equals))

由于一個方法引用可能會被解析為多種函數接口,因此如果我們實現很多參數不同的as方法,那么很容易造成混淆。更好的方式則是在方法名中加入函數參數的類型來區分。

?
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.util.function.*;
 
public class FunctionCastUtil {
 public static <T, U> BiConsumer<T, U> asBiConsumer(BiConsumer<T, U> biConsumer) {
 return biConsumer;
 }
 public static <T, U, R> BiFunction<T, U, R> asBiFunction(BiFunction<T, U, R> biFunction) {
 return biFunction;
 }
 public static <T> BinaryOperator<T> asBinaryOperator(BinaryOperator<T> binaryOperator) {
 return binaryOperator;
 }
 public static <T, U> BiPredicate<T, U> asBiPredicate(BiPredicate<T, U> biPredicate) {
 return biPredicate;
 }
 public static BooleanSupplier asBooleanSupplier(BooleanSupplier booleanSupplier) {
 return booleanSupplier;
 }
 public static <T> Consumer<T> asConsumer(Consumer<T> consumer) {
 return consumer;
 }
 public static DoubleBinaryOperator asDoubleBinaryOperator(DoubleBinaryOperator doubleBinaryOperator) {
 return doubleBinaryOperator;
 }
 public static DoubleConsumer asDoubleConsumer(DoubleConsumer doubleConsumer) {
 return doubleConsumer;
 }
 public static <R> DoubleFunction<R> asDoubleFunction(DoubleFunction<R> doubleFunction) {
 return doubleFunction;
 }
 public static DoublePredicate asDoublePredicate(DoublePredicate doublePredicate) {
 return doublePredicate;
 }
 public static DoubleToIntFunction asDoubleToIntFunction(DoubleToIntFunction doubleToIntFunctiontem) {
 return doubleToIntFunctiontem;
 }
 public static DoubleToLongFunction asDoubleToLongFunction(DoubleToLongFunction doubleToLongFunction) {
 return doubleToLongFunction;
 }
 public static DoubleUnaryOperator asDoubleUnaryOperator(DoubleUnaryOperator doubleUnaryOperator) {
 return doubleUnaryOperator;
 }
 public static <T, R> Function<T, R> asFunction(Function<T, R> function) {
 return function;
 }
 public static IntBinaryOperator asIntBinaryOperator(IntBinaryOperator intBinaryOperator) {
 return intBinaryOperator;
 }
 public static IntConsumer asIntConsumer(IntConsumer intConsumer) {
 return intConsumer;
 }
 public static <R> IntFunction<R> asIntFunction(IntFunction<R> intFunction) {
 return intFunction;
 }
 public static IntPredicate asIntPredicate(IntPredicate intPredicate) {
 return intPredicate;
 }
 public static IntSupplier asIntSupplier(IntSupplier intSupplier) {
 return intSupplier;
 }
 public static IntToDoubleFunction asIntToDoubleFunction(IntToDoubleFunction intToDoubleFunction) {
 return intToDoubleFunction;
 }
 public static IntToLongFunction asIntToLongFunction(IntToLongFunction intToLongFunction) {
 return intToLongFunction;
 }
 public static IntUnaryOperator asIntUnaryOperator(IntUnaryOperator intUnaryOperator) {
 return intUnaryOperator;
 }
 public static LongBinaryOperator asLongBinaryOperator(LongBinaryOperator longBinaryOperator) {
 return longBinaryOperator;
 }
 public static LongConsumer asLongConsumer(LongConsumer longConsumer) {
 return longConsumer;
 }
 public static <R> LongFunction<R> asLongFunction(LongFunction<R> longFunction) {
 return longFunction;
 }
 public static LongPredicate asLongPredicate(LongPredicate longPredicate) {
 return longPredicate;
 }
 public static <T> LongSupplier asLongSupplier(LongSupplier longSupplier) {
 return longSupplier;
 }
 public static LongToDoubleFunction asLongToDoubleFunction(LongToDoubleFunction longToDoubleFunction) {
 return longToDoubleFunction;
 }
 public static LongToIntFunction asLongToIntFunction(LongToIntFunction longToIntFunction) {
 return longToIntFunction;
 }
 public static LongUnaryOperator asLongUnaryOperator(LongUnaryOperator longUnaryOperator) {
 return longUnaryOperator;
 }
 public static <T> ObjDoubleConsumer<T> asObjDoubleConsumer(ObjDoubleConsumer<T> objDoubleConsumer) {
 return objDoubleConsumer;
 }
 public static <T> ObjIntConsumer<T> asObjIntConsumer(ObjIntConsumer<T> objIntConsumer) {
 return objIntConsumer;
 }
 public static <T> ObjLongConsumer<T> asObjLongConsumer(ObjLongConsumer<T> objLongConsumer) {
 return objLongConsumer;
 }
 public static <T> Predicate<T> asPredicate(Predicate<T> predicate) {
 return predicate;
 }
 public static <T> Supplier<T> asSupplier(Supplier<T> supplier) {
 return supplier;
 }
 public static <T, U> ToDoubleBiFunction<T, U> asToDoubleBiFunction(ToDoubleBiFunction<T, U> toDoubleBiFunction) {
 return toDoubleBiFunction;
 }
 public static <T> ToDoubleFunction<T> asToDoubleFunction(ToDoubleFunction<T> toDoubleFunction) {
 return toDoubleFunction;
 }
 public static <T, U> ToIntBiFunction<T, U> asToIntBiFunction(ToIntBiFunction<T, U> toIntBiFunction) {
 return toIntBiFunction;
 }
 public static <T> ToIntFunction<T> asToIntFunction(ToIntFunction<T> ioIntFunction) {
 return ioIntFunction;
 }
 public static <T, U> ToLongBiFunction<T, U> asToLongBiFunction(ToLongBiFunction<T, U> toLongBiFunction) {
 return toLongBiFunction;
 }
 public static <T> ToLongFunction<T> asToLongFunction(ToLongFunction<T> toLongFunction) {
 return toLongFunction;
 }
 public static <T> UnaryOperator<T> asUnaryOperator(UnaryOperator<T> unaryOperator) {
 return unaryOperator;
 }
 private FunctionCastUtil() {
 }
}
 
Stream.of("A", "", "B").filter(asPredicate(String::isEmpty).negate()).count();

英文原文:https://dzone.com/articles/put-your-java-8-method-references-to-work

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:http://www.rowkey.me/blog/2017/09/02/java8-method-reference-work/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 女人扒开下面让男人桶爽视频 | 成人福利在线视频免费观看 | 国内精品久久久久影院男同志 | 久久热这里面只有精品 | 免费一看一级欧美 | 香蕉视频在线观看网址 | 久久99re2在线视频精品 | 99国产自偷色久 | 久久一本综合 | 粗暴hd另类另类 | 久久毛片网站 | 亚洲午夜天堂 | 特黄特级高清免费视频毛片 | 香蕉在线播放 | 成年人免费观看视频网站 | 久久夜色噜噜噜亚洲AV0000 | 884aa在线看片| 奇米影视在线观看 | 视频污版| 精品一久久香蕉国产二月 | 欧美亚洲天堂网 | 国产激情视频在线 | 国产91成人精品亚洲精品 | anal22日本人视频 | 国模一区二区三区视频一 | juliaann大战黑人| 色天天综合色天天看 | 日本动漫打扑克动画片樱花动漫 | 91制片厂果冻传媒杨柳作品 | 99久久免费看国产精品 | zozo日本另类极品 | 国内精品久久久久久野外 | 小向美奈子av | 日本888xxxx| ai换脸杨颖被啪在线观看 | 3d动漫h在线观看网站蜜芽 | 青青久久久 | 亚洲男gay同性同志 亚洲免费在线看 | 男同精品视频免费观看网站 | 男女羞羞的视频 | 欧美成人精品福利网站 |