亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

簡(jiǎn)單易懂的java8新特性之lambda表達(dá)式知識(shí)總結(jié)

瀏覽:107日期:2022-08-12 08:16:55
目錄一、概念二、用法比較2.1 實(shí)現(xiàn)類2.2 匿名類2.3 Lambda三、基本用法3.1 無(wú)參數(shù)無(wú)返回值接口方法3.2 一個(gè)參數(shù)無(wú)返回值接口方法3.3 多個(gè)參數(shù)無(wú)返回值接口方法3.4 無(wú)參數(shù)有返回值接口方法3.5 一個(gè)參數(shù)有返回值接口方法3.6 多個(gè)參數(shù)有返回值接口方法四、函數(shù)引用4.1 靜態(tài)方法引用4.2 非靜態(tài)方法引用4.3 構(gòu)造方法的引用4.4 對(duì)象方法的特殊引用五、注意一、概念

從本質(zhì)上來(lái)說(shuō),它就是一個(gè)匿名函數(shù),可以用來(lái)直接實(shí)現(xiàn)接口中的方法,從而簡(jiǎn)化代碼。但是Lambda有一個(gè)限制,不能實(shí)現(xiàn)接口中的所有方法,所以Lambda表達(dá)式只能用于有且僅有一個(gè)必須需要實(shí)現(xiàn)的方法接口,這里需要注意必須需要實(shí)現(xiàn)這六個(gè)字。

public interface Printer {//有一個(gè)需要實(shí)現(xiàn)的方法,可以使用Lambda表達(dá)式void print();}

public interface Printer {//有一個(gè)需要實(shí)現(xiàn)的方法,可以使用Lambda表達(dá)式void print();//這里雖然有一個(gè)方法,但接口提供了默認(rèn)實(shí)現(xiàn),因此不是必須要實(shí)現(xiàn)的default void printDetail(){}}

public interface Printer {//有一個(gè)需要實(shí)現(xiàn)的方法,可以使用Lambda表達(dá)式void print();//這里雖然有一個(gè)需要實(shí)現(xiàn)的方法,但不是必須要實(shí)現(xiàn)的,因?yàn)閠oString()是Object類中的.String toString();}

public interface Printer {//有一個(gè)需要實(shí)現(xiàn)的方法,可以使用Lambda表達(dá)式void print();//這里雖然有一個(gè)需要實(shí)現(xiàn)的方法,但不是必須要實(shí)現(xiàn)的,因?yàn)閠oString()是Object類中的.String toString();}

像這種只有一個(gè)必須要實(shí)現(xiàn)的方法的接口,在java8中稱之為函數(shù)式接口,在定義接口時(shí)可以在接口名上方加上@FunctionInterface標(biāo)簽,用于驗(yàn)證此接口是否為函數(shù)式接口。如果這個(gè)接口定義好之后不是函數(shù)式接口,那么接口名處會(huì)報(bào)錯(cuò)。在使用Lambda表達(dá)式的時(shí)候,不需要關(guān)注方法名,只需要關(guān)注方法參數(shù)和返回值即可。基本語(yǔ)法很簡(jiǎn)單:

(參數(shù)列表)->{方法體};二、用法比較

java中實(shí)現(xiàn)接口的方式在java8之前有兩種:定義接口的實(shí)現(xiàn)類,使用匿名類,但Lambda表達(dá)式相比于這種方法都簡(jiǎn)單很多。以上文的Printer接口為例,實(shí)現(xiàn)如下:

2.1 實(shí)現(xiàn)類

class PrinterImpl implements Printer{@Overridepublic void print() {System.out.println('Hello World');}}2.2 匿名類

class PrinterAnonymous {Printer printer = new Printer() {@Overridepublic void print() {System.out.println('Hello World');}};}2.3 Lambda

class PrinterLambda{Printer p = ()-> System.out.println('Hello World');}

比較上文三種實(shí)現(xiàn)方式,很顯示Lambda的實(shí)現(xiàn)比前兩種簡(jiǎn)單很多。

三、基本用法3.1 無(wú)參數(shù)無(wú)返回值接口方法

@FunctionalInterfacepublic interface Printer {void print();}public class Tester {public static void main(String[] args) {// 方法一,無(wú)返回值的情況,方法體只有一條語(yǔ)句,可以省略大括號(hào)Printer p1 = () -> System.out.println('Hello World 1');p1.print();// 方法二,標(biāo)準(zhǔn)定義Printer p2 = () -> {System.out.println('Hello World 2');};p2.print();}}3.2 一個(gè)參數(shù)無(wú)返回值接口方法

@FunctionalInterfacepublic interface Printer {void print(String str);}public class Tester {public static void main(String[] args) {// 方法一,無(wú)返回值的情況,方法體只有一條語(yǔ)句,可以省略大括號(hào)//因?yàn)檫@里只有一個(gè)參數(shù),小括號(hào)也可以省略,小括號(hào)省略的前提是:有且僅有一個(gè)參數(shù)//Printer p1 = s -> System.out.println(s);Printer p1 = (s) -> System.out.println(s);p1.print('Hello World 1');// 方法二,無(wú)返回值的情況,方法體只有一條語(yǔ)句,可以省略大括號(hào)Printer p2 = (String s) -> System.out.println(s);p2.print('Hello World 2');// 方法三,標(biāo)準(zhǔn)定義Printer p3 = (String s) -> {System.out.println(s);};p3.print('Hello World 3');}}3.3 多個(gè)參數(shù)無(wú)返回值接口方法

@FunctionalInterfacepublic interface Printer {void print(String str1,String str2);}public class Tester {public static void main(String[] args) {// 方法一,無(wú)返回值的情況,方法體只有一條語(yǔ)句,可以省略大括號(hào)//參Printer p1 = (s1,s2) -> System.out.println(s1+' '+s2);p1.print('Hello World 1','Java 1');// 方法二,無(wú)返回值的情況,方法體只有一條語(yǔ)句,可以省略大括號(hào)Printer p2 = (String s1,String s2) -> System.out.println(s1+' '+s2);p2.print('Hello World 2','Java 2');// 方法三,標(biāo)準(zhǔn)定義Printer p3 = (String s1,String s2) -> {System.out.println(s1+' '+s2);};p3.print('Hello World 3','Java 3');}}3.4 無(wú)參數(shù)有返回值接口方法

@FunctionalInterfacepublic interface Printer {boolean print();}public class Tester {public static void main(String[] args) {// 方法一,有返回值的情況,只有一條語(yǔ)句,return關(guān)鍵字的有無(wú)決定能否活力大括號(hào)Printer p1 = () -> true;boolean has1 = p1.print();System.out.println(has1);//測(cè)試返回結(jié)果// 方法二,標(biāo)準(zhǔn)定義Printer p2 = () -> {return true;};boolean has2 = p2.print();System.out.println(has2);//測(cè)試返回結(jié)果}}3.5 一個(gè)參數(shù)有返回值接口方法

@FunctionalInterfacepublic interface Printer {boolean print(boolean good);}public class Tester {public static void main(String[] args) {// 方法一,有返回值的情況,只有一條語(yǔ)句,return關(guān)鍵字的有無(wú)決定能否活力大括號(hào) //因?yàn)檫@里只有一個(gè)參數(shù),小括號(hào)也可以省略,小括號(hào)省略的前提是:有且僅有一個(gè)參數(shù) //Printer p1 = good -> good;Printer p1 = (good) -> good;boolean has1 = p1.print(true);System.out.println(has1);// 方法二,標(biāo)準(zhǔn)定義Printer p2 = (good) -> {return good;};boolean has2 = p2.print(false);System.out.println(has2);}}3.6 多個(gè)參數(shù)有返回值接口方法

@FunctionalInterfacepublic interface Printer {boolean print(boolean good1,boolean good2);}public class Tester {public static void main(String[] args) {// 方法一,有返回值的情況,只有一條語(yǔ)句,return關(guān)鍵字的有無(wú)決定能否活力大括號(hào)Printer p1 = (good1,good2) -> good1;boolean has1 = p1.print(true,false);System.out.println(has1);// 方法二,標(biāo)準(zhǔn)定義Printer p2 = (good1,good2) -> {return good1;};boolean has2 = p2.print(false,false);System.out.println(has2);}}四、函數(shù)引用

在實(shí)現(xiàn)一個(gè)接口的方法時(shí),如果現(xiàn)有的其他地方的某個(gè)函數(shù)已經(jīng)實(shí)現(xiàn)了接口方法的邏輯,可以使用方法引用直接將這個(gè)邏輯引用過來(lái)。

4.1 靜態(tài)方法引用

語(yǔ)法:

接口名 變量名 = 類 ::已實(shí)現(xiàn)的方法

注意事項(xiàng):

在引用的方法后面,不要添加小括號(hào) 引用的這個(gè)方法,參數(shù)和返回值,必須要跟接口中定義的一致

示例:Printer 需要實(shí)現(xiàn)的方法在Checker中有同樣的實(shí)現(xiàn),這樣就可以直接引用過來(lái)

@FunctionalInterfacepublic interface Printer {String print(boolean good1,boolean good2);}public class Checker {public static String check(boolean a,boolean b) {if(a && b) {return 'Java is good';}else if (!a && b) {return 'Java is better';}return 'Java is best';}}public class Tester {public static void main(String[] args) {Printer p1 = Checker::check;//用類名來(lái)引用System.out.println(p1.print(true, true));}}4.2 非靜態(tài)方法引用

語(yǔ)法:

接口名 變量名 = 對(duì)象 ::靜態(tài)方法

注意事項(xiàng):

在引用的方法后面,不要添加小括號(hào) 引用的這個(gè)方法,參數(shù)和返回值,必須要跟接口中定義的一致

示例:Printer 需要實(shí)現(xiàn)的方法在Checker中有同樣的實(shí)現(xiàn),這樣就可以直接引用過來(lái)

@FunctionalInterfacepublic interface Printer {String print(boolean good1,boolean good2);}public class Checker {public String check(boolean a,boolean b) {if(a && b) {return 'Java is good';}else if (!a && b) {return 'Java is better';}return 'Java is best';}}public class Tester {public static void main(String[] args) {Printer p1 = new Checker()::check;//必須用對(duì)象來(lái)引用System.out.println(p1.print(true, true));}}4.3 構(gòu)造方法的引用

如果一個(gè)函數(shù)式接口中定義的方法僅僅是為了得到一個(gè)對(duì)象,此時(shí)我們就可以使用構(gòu)造方法的引用,簡(jiǎn)化這個(gè)方法的實(shí)現(xiàn)語(yǔ)法:

接口名 變量名 = 類名 ::new

注意事項(xiàng):可以通過接口中的方法參數(shù),區(qū)分引用不同的構(gòu)造方法示例:

@FunctionalInterfacepublic interface Printer1 {Checker getCheck();}@FunctionalInterfacepublic interface Printer2 {Checker getCheck(int a);}public class Checker {int times;public Checker() {System.out.println('I am none parameter');}public Checker(int a) {System.out.println('I have one parameter');}}public class Tester {public static void main(String[] args) {//引用無(wú)參構(gòu)造方法Printer1 p1 = Checker::new;p1.getCheck();//引用有參構(gòu)造方法Printer2 p2 = Checker::new;p2.getCheck(1);}}4.4 對(duì)象方法的特殊引用

如果實(shí)現(xiàn)某些接口的時(shí)候,Lambda表達(dá)式中包含了某一個(gè)對(duì)象,此時(shí)方法體中,直接使用這個(gè)對(duì)象調(diào)用它的某一個(gè)方法就可以完成整個(gè)的邏輯。其他的參數(shù),可以作為調(diào)用方法的參數(shù)。此時(shí),可以對(duì)這種實(shí)現(xiàn)進(jìn)行簡(jiǎn)化。示例:

@FunctionalInterfacepublic interface Printer1 {int getCheck(Checker checker);}@FunctionalInterfacepublic interface Printer2 {void setCheck(Checker checker, int a);}public class Tester {public static void main(String[] args) {Checker checker = new Checker();checker.setTimes(100);// 沒有簡(jiǎn)化前,按照之前的方法使用lambda表達(dá)式Printer1 p1 = x -> x.getTimes();System.out.println(p1.getCheck(checker));//測(cè)試// 簡(jiǎn)化之后Printer1 p11 = Checker::getTimes;System.out.println(p11.getCheck(checker));//測(cè)試// 沒有簡(jiǎn)化前,按照之前的方法使用lambda表達(dá)式Printer2 p2 = (x,y)-> x.setTimes(y);p2.setCheck(checker, 50);System.out.println(checker.getTimes());//測(cè)試// 簡(jiǎn)化之后Printer2 p22 = Checker::setTimes;p22.setCheck(checker, 30);System.out.println(checker.getTimes());//測(cè)試}}五、注意

當(dāng)在Lambda表達(dá)式中使用了某一個(gè)局部變量,那么這個(gè)局部變量的值在Lambda表達(dá)式之外,不可以被改變,因?yàn)槟J(rèn)將其定義成final常量。但全局變量變量沒有這方面的限制。示例:

@FunctionalInterfacepublic interface Printer {void setTime();}public class Tester {public static void main(String[] args) {int time = 10;Printer p = () -> System.out.println(time);//這里出錯(cuò)了,因?yàn)橄乱恍袑?duì)time進(jìn)行修改time = 15;//這里的值不能改變,會(huì)導(dǎo)致上一行出錯(cuò)}}

基本概括了Lambda表達(dá)式的所有用法,不足之處,請(qǐng)諒解,謝謝!

到此這篇關(guān)于簡(jiǎn)單易懂的java8新特性之lambda表達(dá)式知識(shí)總結(jié)的文章就介紹到這了,更多相關(guān)java lambda表達(dá)式內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 一级特黄欧美 | 日本韩国一级 | 亚洲综合伦理一区 | 国产亚洲欧美成人久久片 | 91成人免费在线视频 | 白云精品视频国产专区 | 中文字幕视频在线观看 | 毛片免费看看 | 精品国产免费第一区二区三区日韩 | 国内精品久久久久不卡 | 最新三级网址 | 国产亚洲自拍一区 | 毛片96视频免费观看 | 久久久久久久国产精品毛片 | 国产精品区在线12p 国产精品人成 | 一本久道久久综合中文字幕 | 欧美日韩亚洲综合另类ac | 亚洲男女网站 | 中文字幕乱码视频32 | 国产男人天堂 | 国产一级一级一级成人毛片 | 在线观看日本永久免费视频 | 欧美一级片播放 | 免费在线一区二区三区 | 免费观看日本视频 | 在线播放高清国语自产拍免费 | 亚洲一级毛片免费观看 | 91精品久久久久含羞草 | 全免费a级毛片免费看视频免 | 成人性生免费视频 | 在线综合+亚洲+欧美中文字幕 | 农村三级孕妇视频在线 | 337p粉嫩大胆噜噜噜鲁 | 久久久精品视频免费观看 | 一级毛片在线观看视频 | 视频一区二区三区在线 | 日韩一级伦理片 | 免费看成人片 | 波多野结衣一级片 | 黄色毛片视频校园交易 | 美女免费在线视频 |