淺談Java中Lambda表達(dá)式的相關(guān)操作
為什么要使用Lambda?
可以對一個(gè)接口進(jìn)行非常簡潔的實(shí)現(xiàn)。
Lambda對接口的要求?
接口中定義的抽象方法有且只有一個(gè)才可以。
傳統(tǒng)實(shí)現(xiàn)一個(gè)接口需要這樣做:
方法一:
// 實(shí)現(xiàn)接口,同時(shí)必須重寫接口中抽象方法 class Test implements IntrfacefN { @Override public void getUser(int a, int b) { } } // @FunctionalInterface 注解意思:函數(shù)式接口,用來做規(guī)范,有這個(gè)注解,說明此接口有且只有一個(gè)抽象方法?。?! @FunctionalInterface interface IntrfacefN{ public void getUser(int a, int b); }
方法二:
匿名表達(dá)式
public class Lamda { public static void main(String[] args) { // 匿名表達(dá)式實(shí)現(xiàn)接口 IntrfacefN intrfacefN1 = new IntrfacefN(){ @Override public void getUser(int a, int b) { } }; } }
使用Lambda -> 只關(guān)注參數(shù)和方法體(返回值類型不需要寫、類型不需要寫)
public class Lamda { public static void main(String[] args) { // 實(shí)現(xiàn)接口,后邊匿名函數(shù)就是重寫的方法! IntrfacefN intrfacefN = (int a, int b) -> System.out.println(a-b); intrfacefN.getUser(1, 2); } }
不定參
@FunctionalInterface interface IntrfacefN{ public void getUser(int... a); } public class Lamda { public static void main(String[] args) { IntrfacefN intrfacefN = (int ...a) -> { for (int i = 0; i < a.length; i ++) { System.out.println(a[i]); } }; intrfacefN.getUser(1, 2); } }
可省略的部分
參數(shù)類型
IntrfacefN intrfacefN = (a, b) -> System.out.println(a-b);
小括號
前提只有一個(gè)參數(shù)情況
IntrfacefN intrfacefN = a -> System.out.println(a);
方法大括號
方法體只有一句代碼
IntrfacefN intrfacefN = (a, b) -> System.out.println(a-b);
返回return
如果大括號中只有一條返回語句,則return 也可以省略
IntrfacefN intrfacefN = (a, b) -> { return a-b }; // 省略之后寫法: IntrfacefN intrfacefN = (a, b) -> a-b;
高級部分
方法的引用
將一個(gè)Lambda表達(dá)式的實(shí)現(xiàn)指向一個(gè)已實(shí)現(xiàn)的方法,這樣做相當(dāng)于公共邏輯部分的抽離,實(shí)現(xiàn)復(fù)用。
public class Lamda { public static void main(String[] args) { IntrfacefN intrfacefN = (a, b) -> add(a, b); intrfacefN.getUser(1, 2); } public static void add(int a, int b) { System.out.println(a+b); } } @FunctionalInterface interface IntrfacefN{ public void getUser(int a, int b); }
還有更簡潔的實(shí)現(xiàn):
方法隸屬者:語法 - 方法隸屬者::方法名
補(bǔ)充下:這個(gè)方法隸屬者,主要看方法是類方法還是對象方法,如果是類 - 方法類::方法名 ,如果是對象方法 - new 方法類::方法名
public class Lamda { public static void main(String[] args) { IntrfacefN intrfacefN = Lamda::add; intrfacefN.getUser(1, 2); } public static void add(int a, int b) { System.out.println(a+b); } } @FunctionalInterface interface IntrfacefN{ public void getUser(int a, int b); }
到此這篇關(guān)于淺談Java中Lambda表達(dá)式的相關(guān)操作的文章就介紹到這了,更多相關(guān)Java Lambda表達(dá)式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)自定義日期選擇器的方法實(shí)例
日期選擇器是我們?nèi)粘i_發(fā)中經(jīng)常需要用到的一個(gè)功能,下面這篇文章主要給大家介紹了關(guān)于利用java實(shí)現(xiàn)自定義日期選擇器的相關(guān)資料,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10java實(shí)現(xiàn)數(shù)據(jù)庫的數(shù)據(jù)寫入到txt的方法
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)數(shù)據(jù)庫的數(shù)據(jù)寫入到txt的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07解決SpringBoot項(xiàng)目使用多線程處理任務(wù)時(shí)無法通過@Autowired注入bean問題
這篇文章主要介紹了SpringBoot項(xiàng)目使用多線程處理任務(wù)時(shí)無法通過@Autowired注入bean問題的解決方法,需要的朋友可以參考下2018-09-09Java并發(fā)編程之關(guān)鍵字volatile的深入解析
提高java的并發(fā)編程,就不得不提volatile關(guān)鍵字,不管是在面試還是實(shí)際開發(fā)中volatile都是一個(gè)應(yīng)該掌握的技能,這篇文章主要給大家介紹了關(guān)于Java并發(fā)編程之關(guān)鍵字volatile的相關(guān)資料,需要的朋友可以參考下2021-09-09Java Stream的基本概念以及創(chuàng)建方法
這篇文章主要介紹了Java Stream的基本概念以及創(chuàng)建方法,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下2020-08-08