淺談Java中Lambda表達式的相關操作
為什么要使用Lambda?
可以對一個接口進行非常簡潔的實現(xiàn)。
Lambda對接口的要求?
接口中定義的抽象方法有且只有一個才可以。
傳統(tǒng)實現(xiàn)一個接口需要這樣做:
方法一:
// 實現(xiàn)接口,同時必須重寫接口中抽象方法
class Test implements IntrfacefN {
@Override
public void getUser(int a, int b) {
}
}
// @FunctionalInterface 注解意思:函數(shù)式接口,用來做規(guī)范,有這個注解,說明此接口有且只有一個抽象方法?。?!
@FunctionalInterface
interface IntrfacefN{
public void getUser(int a, int b);
}
方法二:
匿名表達式
public class Lamda {
public static void main(String[] args) {
// 匿名表達式實現(xiàn)接口
IntrfacefN intrfacefN1 = new IntrfacefN(){
@Override
public void getUser(int a, int b) {
}
};
}
}
使用Lambda -> 只關注參數(shù)和方法體(返回值類型不需要寫、類型不需要寫)
public class Lamda {
public static void main(String[] args) {
// 實現(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);
小括號
前提只有一個參數(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;
高級部分
方法的引用
將一個Lambda表達式的實現(xiàn)指向一個已實現(xiàn)的方法,這樣做相當于公共邏輯部分的抽離,實現(xiàn)復用。
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);
}
還有更簡潔的實現(xiàn):
方法隸屬者:語法 - 方法隸屬者::方法名
補充下:這個方法隸屬者,主要看方法是類方法還是對象方法,如果是類 - 方法類::方法名 ,如果是對象方法 - 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);
}
到此這篇關于淺談Java中Lambda表達式的相關操作的文章就介紹到這了,更多相關Java Lambda表達式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot返回統(tǒng)一的JSON標準格式實現(xiàn)步驟
這篇文章主要介紹了SpringBoot返回統(tǒng)一的JSON標準格式,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08
java Volatile與Synchronized的區(qū)別
這篇文章主要介紹了java Volatile與Synchronized的區(qū)別,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-12-12
java?SpringBoot?分布式事務的解決方案(JTA+Atomic+多數(shù)據(jù)源)
這篇文章主要介紹了java?SpringBoot?分布式事務的解決方案(JTA+Atomic+多數(shù)據(jù)源),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-08-08
Nacos啟動出現(xiàn)failed to req API:/nacos/v1/ns/insta
這篇文章主要介紹了Nacos啟動出現(xiàn)failed to req API:/nacos/v1/ns/instance after all servers問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
詳解MybatisPlus中@TableLogic注解的使用
@TableLogic一般用于實現(xiàn)數(shù)據(jù)庫數(shù)據(jù)邏輯刪除,本文我們將介紹 @TableLogic 注解的用法,以及每個屬性的實際意義和用法,感興趣的可以了解一下2022-06-06

