簡(jiǎn)單易懂的java8新特性之lambda表達(dá)式知識(shí)總結(jié)
從本質(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)!
相關(guān)文章:
1. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法2. html小技巧之td,div標(biāo)簽里內(nèi)容不換行3. nestjs實(shí)現(xiàn)圖形校驗(yàn)和單點(diǎn)登錄的示例代碼4. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式5. python實(shí)現(xiàn)自動(dòng)化辦公郵件合并功能6. python開發(fā)飛機(jī)大戰(zhàn)游戲7. laravel ajax curd 搜索登錄判斷功能的實(shí)現(xiàn)8. css進(jìn)階學(xué)習(xí) 選擇符9. Echarts通過dataset數(shù)據(jù)集實(shí)現(xiàn)創(chuàng)建單軸散點(diǎn)圖10. Python 如何將integer轉(zhuǎn)化為羅馬數(shù)(3999以內(nèi))
