激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

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

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

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

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

前言

方法引用是用來(lái)直接訪問(wèn)類(lèi)或者實(shí)例的已經(jīng)存在的方法或者構(gòu)造方法。方法引用提供了一種引用而不執(zhí)行方法的方式,它需要由兼容的函數(shù)式接口構(gòu)成的目標(biāo)類(lèi)型上下文。計(jì)算時(shí),方法引用會(huì)創(chuàng)建函數(shù)式接口的一個(gè)實(shí)例。

當(dāng)Lambda表達(dá)式中只是執(zhí)行一個(gè)方法調(diào)用時(shí),不用Lambda表達(dá)式,直接通過(guò)方法引用的形式可讀性更高一些。方法引用是一種更簡(jiǎn)潔易懂的Lambda表達(dá)式。

注意:方法引用是一個(gè)Lambda表達(dá)式,其中方法引用的操作符是雙冒號(hào)"::"。

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

首先看一個(gè)使用方法引用的例子:

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

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

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

Java8中的Predicate,有predicate.negate()可以轉(zhuǎn)換為斷言的否定形式,但String::isEmpty卻無(wú)法這么做(String::isEmpty.negate()或者!String::isEmpty )。因?yàn)榉椒ㄒ貌⒉皇且粋€(gè)lambda或者函數(shù)接口,它能夠被解析為一個(gè)或者多個(gè)函數(shù)接口。如,String::isEmpty至少可以被解析如下:

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

為了解決上述的問(wèn)題,我們可以通過(guò)某種機(jī)制顯式地將方法引用轉(zhuǎn)換為一個(gè)函數(shù)接口:

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

通過(guò)使用一個(gè)靜態(tài)方法,接受方法引用參數(shù),返回一個(gè)函數(shù)接口,即可實(shí)現(xiàn)方法引用到函數(shù)接口的轉(zhuǎn)換。接著,我們就可以使用方法引用來(lái)實(shí)現(xiàn)上面例子中的獲取非空字符串的數(shù)目。

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

進(jìn)一步還能使用各種組合的Predicate。

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

由于一個(gè)方法引用可能會(huì)被解析為多種函數(shù)接口,因此如果我們實(shí)現(xiàn)很多參數(shù)不同的as方法,那么很容易造成混淆。更好的方式則是在方法名中加入函數(shù)參數(shù)的類(lèi)型來(lái)區(qū)分。

?
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

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)服務(wù)器之家的支持。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 加勒比婷婷色综合久久 | 免费a级观看 | 1314成人网 | 国产精品成人一区 | 国产亚洲精品久久久久5区 日韩一级片一区二区三区 国产精品久久久久av | 亚洲四播房 | 亚洲情在线 | 日韩av片网站 | 欧美成人一级片 | 国产高潮好爽好大受不了了 | 性生活视频软件 | 精品成人av一区二区三区 | 久久精品国产亚洲一区二区 | av黄色片网站 | 国产色91 | 91免费高清视频 | 97久久精品一区二区三区观看 | 欧美www| 久久久久亚洲国产精品 | 久久精品国产精品亚洲 | 91香蕉国产亚洲一区二区三区 | 失禁高潮抽搐喷水h | 奇米影视奇奇米色狠狠色777 | 视频一区二区三区在线 | 国产亚洲高清在线精品不卡 | 99视频观看| 黄网站免费在线看 | 亚洲欧美一区二区三区在线观看 | videos真实高潮xxxx | 91精品中文字幕 | 欧美性色黄大片www 成人免费网站在线观看 | 高清国产午夜精品久久久久久 | 97久色| 欧美成人一区在线观看 | 一本色道久久综合亚洲精品小说 | 91看片成人 | 成人免费观看在线 | 黄视频网站免费在线观看 | 久草在线视频福利 | 国产在线看一区 | 国产色爱综合网 |