Java Lambda表達(dá)式超詳細(xì)介紹
一、背景
Lambda表達(dá)式是Java SE 8中一個(gè)重要的新特性。lambda表達(dá)式允許你通過表達(dá)式來代替功能接口。 lambda表達(dá)式就和方法一樣,它提供了一個(gè)正常的參數(shù)列表和一個(gè)使用這些參數(shù)的主體(body,可以是一個(gè)表達(dá)式或一個(gè)代碼
塊)。 Lambda 表達(dá)式(Lambda expression)可以看作是一個(gè)匿名函數(shù),基于數(shù)學(xué)中的λ演算得名,也可稱為閉包(Closure)
1.Lambda表達(dá)式的語法
基本語法: (parameters) -> expression 或 (parameters) ->{ statements; }
Lambda表達(dá)式由三部分組成:
- 1.paramaters:類似方法中的形參列表,這里的參數(shù)是函數(shù)式接口里的參數(shù)。這里的參數(shù)類型可以明確的聲明 也可不聲明而由JVM隱含的推斷。另外當(dāng)只有一個(gè)推斷類型時(shí)可以省略掉圓括號。
- 2.->:可理解為“被用于”的意思
- 3.方法體:可以是表達(dá)式也可以代碼塊,是函數(shù)式接口里方法的實(shí)現(xiàn)。代碼塊可返回一個(gè)值或者什么都不反 回,這里的代碼塊塊等同于方法的方法體。如果是表達(dá)式,也可以返回一個(gè)值或者什么都不反回。
// 1. 不需要參數(shù),返回值為 2 ()->2 // 2. 接收一個(gè)參數(shù)(數(shù)字類型),返回其2倍的值 x->2*x // 3. 接受2個(gè)參數(shù)(數(shù)字),并返回他們的和 (x,y) -> x+y // 4. 接收2個(gè)int型整數(shù),返回他們的乘積 (int x,int y) -> x * y // 5. 接受一個(gè) string 對象,并在控制臺打印,不返回任何值(看起來像是返回void) (String s) -> System.out.print(s)
2.函數(shù)式接口
要了解Lambda表達(dá)式,首先需要了解什么是函數(shù)式接口,函數(shù)式接口定義:一個(gè)接口有且只有一個(gè)抽象方法 。
注意:
1.如果一個(gè)接口只有一個(gè)抽象方法,那么該接口就是一個(gè)函數(shù)式接口
2.如果我們在某個(gè)接口上聲明了@FunctionalInterface注解,那么編譯器就會按照函數(shù)式接口的定義來要求該接口,這樣如果有兩個(gè)抽象方法,程序編譯就會報(bào)錯(cuò)的。所以,從某種意義上來說,只要你保證你的接口 中只有一個(gè)抽象方法,你可以不加這個(gè)注解。加上就會自動進(jìn)行檢測的。
定義方式:
@FunctionalInterface
interface NoParameterNoReturn {
? ? //注意:只能有一個(gè)抽象方法
? ? void test();
}但是這種方式也是可以的:
@FunctionalInterface
interface NoParameterNoReturn {
? ? void test();
? ? default void test2() {
? ? ? ? System.out.println("JDK1.8新特性,default默認(rèn)方法可以有具體的實(shí)現(xiàn)");
? ? }
}二、Lambda表達(dá)式的基本使用
首先,我們實(shí)現(xiàn)準(zhǔn)備好幾個(gè)接口:
@FunctionalInterface
interface NoParameterNoReturn {
? ? //注意:只能有一個(gè)抽象方法
? ? void test();
}
//無返回值一個(gè)參數(shù)
@FunctionalInterface
interface OneParameterNoReturn {
? ? void test(int a);
}
//無返回值多個(gè)參數(shù)
@FunctionalInterface
interface MoreParameterNoReturn {
? ? void test(int a, int b);
}
//有返回值無參數(shù)
@FunctionalInterface
interface NoParameterReturn {
? ? int test();
}
//有返回值一個(gè)參數(shù)
@FunctionalInterface
interface OneParameterReturn {
? ? int test(int a);
}
//有返回值多參數(shù)
@FunctionalInterface
interface MoreParameterReturn {
? ? int test(int a, int b);
}我們在上面提到過,Lambda表達(dá)式本質(zhì)是一個(gè)匿名函數(shù),函數(shù)的方法是:返回值 方法名 參數(shù)列表 方法體。在,Lambda表達(dá)式中我們只需要關(guān)心:參數(shù)列表 方法體。
具體使用見以下示例代碼:
@FunctionalInterface
interface NoParameterNoReturn {
? ? //注意:只能有一個(gè)抽象方法
? ? void test();
}
//無返回值一個(gè)參數(shù)
@FunctionalInterface
interface OneParameterNoReturn {
? ? void test(int a);
}
//無返回值多個(gè)參數(shù)
@FunctionalInterface
interface MoreParameterNoReturn {
? ? void test(int a, int b);
}
//有返回值無參數(shù)
@FunctionalInterface
interface NoParameterReturn {
? ? int test();
}
//有返回值一個(gè)參數(shù)
@FunctionalInterface
interface OneParameterReturn {
? ? int test(int a);
}
//有返回值多參數(shù)
@FunctionalInterface
interface MoreParameterReturn {
? ? int test(int a, int b);
}
public class TestDemo2 {
? ? public static void main(String[] args) {
? ? ? ? NoParameterNoReturn noParameterNoReturn = () -> {
? ? ? ? ? ? System.out.println("無參數(shù)無返回值");
? ? ? ? };
? ? ? ? //test方法的主體內(nèi)容在上述括號內(nèi)
? ? ? ? noParameterNoReturn.test();
? ? ? ? OneParameterNoReturn oneParameterNoReturn = (int a) -> {
? ? ? ? ? ? System.out.println("無參數(shù)一個(gè)返回值:" + a);
? ? ? ? };
? ? ? ? oneParameterNoReturn.test(10);
? ? ? ? MoreParameterNoReturn moreParameterNoReturn = (int a, int b) -> {
? ? ? ? ? ? System.out.println("無返回值多個(gè)參數(shù):" + a + " " + b);
? ? ? ? };
? ? ? ? moreParameterNoReturn.test(20, 30);
? ? ? ? NoParameterReturn noParameterReturn = () -> {
? ? ? ? ? ? System.out.println("有返回值無參數(shù)!");
? ? ? ? ? ? return 40;
? ? ? ? };
? ? ? ? //接收函數(shù)的返回值
? ? ? ? int ret = noParameterReturn.test();
? ? ? ? System.out.println(ret);
? ? ? ? OneParameterReturn oneParameterReturn = (int a) -> {
? ? ? ? ? ? System.out.println("有返回值有參數(shù)!");
? ? ? ? ? ? return a;
? ? ? ? };
? ? ? ? ret = oneParameterReturn.test(50);
? ? ? ? System.out.println(ret);
? ? ? ? MoreParameterReturn moreParameterReturn = (int a, int b) -> {
? ? ? ? ? ? System.out.println("有返回值多個(gè)參數(shù)!");
? ? ? ? ? ? return a + b;
? ? ? ? };
? ? ? ? ret = moreParameterReturn.test(60, 70);
? ? ? ? System.out.println(ret);
? ? }
}三、語法精簡
- 參數(shù)類型可以省略,如果需要省略,每個(gè)參數(shù)的類型都要省略。
- 參數(shù)的小括號里面只有一個(gè)參數(shù),那么小括號可以省略
- 如果方法體當(dāng)中只有一句代碼,那么大括號可以省略
- 如果方法體中只有一條語句,其是return語句,那么大括號可以省略,且去掉return關(guān)鍵字。
示例代碼:
@FunctionalInterface
interface NoParameterNoReturn {
? ? //注意:只能有一個(gè)抽象方法
? ? void test();
}
//無返回值一個(gè)參數(shù)
@FunctionalInterface
interface OneParameterNoReturn {
? ? void test(int a);
}
//無返回值多個(gè)參數(shù)
@FunctionalInterface
interface MoreParameterNoReturn {
? ? void test(int a, int b);
}
//有返回值無參數(shù)
@FunctionalInterface
interface NoParameterReturn {
? ? int test();
}
//有返回值一個(gè)參數(shù)
@FunctionalInterface
interface OneParameterReturn {
? ? int test(int a);
}
//有返回值多參數(shù)
@FunctionalInterface
interface MoreParameterReturn {
? ? int test(int a, int b);
}
public class TestDemo2 {
? ? public static void main(String[] args) {
? ? ? ? //方法參數(shù)有多個(gè)且方法體中無返回值,則可以省略參數(shù)類型
? ? ? ? MoreParameterNoReturn moreParameterNoReturn = (a, b) -> {
? ? ? ? ? ? System.out.println("無返回值多個(gè)參數(shù),省略參數(shù)類型:" + a + " " + b);
? ? ? ? };
? ? ? ? moreParameterNoReturn.test(20, 30);
? ? ? ? //方法中只有一個(gè)參數(shù),那么小括號可以省略
? ? ? ? OneParameterNoReturn oneParameterNoReturn = a -> {
? ? ? ? ? ? System.out.println("方法中只有一個(gè)參數(shù),那么小括號可以省略:" + a);
? ? ? ? };
? ? ? ? oneParameterNoReturn.test(10);
? ? ? ? //無參數(shù)無返回值,方法體中只有 一行代碼的時(shí)候,可以去掉方法體的大括號
? ? ? ? NoParameterNoReturn noParameterNoReturn = () -> System.out.println("無參數(shù)無返回值,方法體中只有 一行代碼");
? ? ? ? noParameterNoReturn.test();
? ? ? ? //方法體中只有一條語句,且是return語句,且無參數(shù)
? ? ? ? NoParameterReturn noParameterReturn = () -> 40;
? ? ? ? int ret = noParameterReturn.test();
? ? ? ? System.out.println(ret);
? ? }
}四、變量捕獲
Lambda 表達(dá)式中存在變量捕獲 ,了解了變量捕獲之后,我們才能更好的理解Lambda 表達(dá)式的作用域 。Java當(dāng)中的匿名類中,會存在變量捕獲。
下面我們來講下在Lambda當(dāng)中也可以進(jìn)行變量的捕獲,具體我們看一下代碼:
@FunctionalInterface
interface NoParameterNoReturn {
? ? void test();
}
public class TestDemo2 {
? ? public static void main(String[] args) {
? ? ? ? int a = 10;
? ? ? ? NoParameterNoReturn noParameterNoReturn = () -> {
? ? ? ? ? ? /*
? ? ? ? ? ? 注意此處不能夠修改a的值,與匿名內(nèi)部類中相同
? ? ? ? ? ? a = 99;
? ? ? ? ? ? */
? ? ? ? ? ? System.out.println("捕獲變量:" + a);
? ? ? ? };
? ? ? ? noParameterNoReturn.test();
? ? }
}五、Lambda在集合當(dāng)中的使用
為了能夠讓Lambda和Java的集合類集更好的一起使用,集合當(dāng)中,也新增了部分接口,以便與Lambda表達(dá)式對接。

以上方法的作用可自行查看我們發(fā)的幫助手冊。我們這里會示例一些方法的使用。注意:Collection的forEach()方法是從接口 java.lang.Iterable 拿過來的。
1.Collection接口
forEach() 方法演示
該方法在接口 Iterable 當(dāng)中,原型如下:

**forEach()**方法表示:對容器中的每個(gè)元素執(zhí)行action指定的動作
可以看到我們的參數(shù)Consumer其實(shí)是一個(gè)函數(shù)式接口:

這個(gè)函數(shù)式接口中有一個(gè)抽象方法accept:

public class TestDemo2 {
? ? public static void main(String[] args) {
? ? ? ? ArrayList<String> list = new ArrayList<>();
? ? ? ? list.add("Hello");
? ? ? ? list.add("bit");
? ? ? ? list.add("hello");
? ? ? ? list.add("lambda");
? ? ? ? list.forEach(new Consumer<String>() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void accept(String s) {
? ? ? ? ? ? ? ? //簡單遍歷集合中的元素
? ? ? ? ? ? ? ? System.out.println(s);
? ? ? ? ? ? }
? ? ? ? });
? ? }
}輸出結(jié)果:
Hello bit hello lambda
我們可以修改為如下代碼:
public class TestDemo2 {
? ? public static void main(String[] args) {
? ? ? ? ArrayList<String> list = new ArrayList<>();
? ? ? ? list.add("Hello");
? ? ? ? list.add("bit");
? ? ? ? list.add("hello");
? ? ? ? list.add("lambda");
? ? ?
? ? ? ? ?list.forEach((String s) -> {
? ? ? ? ? ? System.out.println(s);
? ? ? ? });
? ? }
}同時(shí)還可以簡化代碼:
public class TestDemo2 {
? ? public static void main(String[] args) {
? ? ? ? ArrayList<String> list = new ArrayList<>();
? ? ? ? list.add("Hello");
? ? ? ? list.add("bit");
? ? ? ? list.add("hello");
? ? ? ? list.add("lambda");
? ? ? ? list.forEach(s -> System.out.println(s));
? ? }
}六、List接口
1.sort()方法的演示
sort方法源碼:該方法根據(jù)c指定的比較規(guī)則對容器元素進(jìn)行排序。
可以看到其參數(shù)是Comparator,我們點(diǎn)進(jìn)去看下:又是一個(gè)函數(shù)式接口

這個(gè)接口中有一個(gè)抽象方法叫做compare方法:

使用示例:
public class TestDemo2 {
? ? public static void main(String[] args) {
? ? ? ? ArrayList<String> list = new ArrayList<>();
? ? ? ? list.add("Hello");
? ? ? ? list.add("bit");
? ? ? ? list.add("hello");
? ? ? ? list.add("lambda");
? ? ? ? /*
? ? ? ? 對list集合中的字符串按照長度進(jìn)行排序
? ? ? ? ?*/
? ? ? ? list.sort(new Comparator<String>() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public int compare(String o1, String o2) {
? ? ? ? ? ? ? ? return o1.length() - o2.length();
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? /*
? ? ? ? 輸出排序后最終的結(jié)果
? ? ? ? ?*/
? ? ? ? list.forEach(s -> System.out.println(s));
? ? }
}輸出結(jié)果為:
bit Hello hello lambda
修改為lambda表達(dá)式:
public class TestDemo2 {
? ? public static void main(String[] args) {
? ? ? ? ArrayList<String> list = new ArrayList<>();
? ? ? ? list.add("Hello");
? ? ? ? list.add("bit");
? ? ? ? list.add("hello");
? ? ? ? list.add("lambda");
? ? ? ? /*
? ? ? ? 對list集合中的字符串按照長度進(jìn)行排序
? ? ? ? ?*/
? ? ? ? list.sort((String o1, String o2) -> {
? ? ? ? ? ? ? ? ? ? return o1.length() - o2.length();
? ? ? ? ? ? ? ? }
? ? ? ? );
? ? ? ? /*
? ? ? ? 輸出排序后最終的結(jié)果:
? ? ? ? bit
? ? ? ? Hello
? ? ? ? hello
? ? ? ? lambda
? ? ? ? ?*/
? ? ? ? list.forEach(s -> System.out.println(s));
? ? }
}此時(shí)還可以對代碼進(jìn)行簡化:
public class TestDemo2 {
? ? public static void main(String[] args) {
? ? ? ? ArrayList<String> list = new ArrayList<>();
? ? ? ? list.add("Hello");
? ? ? ? list.add("bit");
? ? ? ? list.add("hello");
? ? ? ? list.add("lambda");
? ? ? ? /*
? ? ? ? 對list集合中的字符串按照長度進(jìn)行排序
? ? ? ? ?*/
? ? ? ? list.sort((o1, o2) ->
? ? ? ? ? ? ? ? o1.length() - o2.length()
? ? ? ? );
? ? ? ? /*
? ? ? ? 輸出排序后最終的結(jié)果:
? ? ? ? bit
? ? ? ? Hello
? ? ? ? hello
? ? ? ? lambda
? ? ? ? ?*/
? ? ? ? list.forEach(s -> System.out.println(s));
? ? }
}七、Map接口
HashMap 的 forEach()方法:

這個(gè)函數(shù)式接口中有一個(gè)抽象方法叫做accept方法:

代碼示例:
public class TestDemo2 {
? ? public static void main(String[] args) {
? ? ? ? HashMap<Integer, String> map = new HashMap<>();
? ? ? ? map.put(1, "hello");
? ? ? ? map.put(2, "bit");
? ? ? ? map.put(3, "hello");
? ? ? ? map.put(4, "lambda");
? ? ? ? map.forEach(new BiConsumer<Integer, String>() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void accept(Integer integer, String s) {
? ? ? ? ? ? ? ? System.out.println(integer + " " + s);
? ? ? ? ? ? }
? ? ? ? });
? ? }
}輸出結(jié)果:
1 hello
2 bit
3 hello
4 lambda
使用lambda表達(dá)式后的代碼:
public class TestDemo2 {
? ? public static void main(String[] args) {
? ? ? ? HashMap<Integer, String> map = new HashMap<>();
? ? ? ? map.put(1, "hello");
? ? ? ? map.put(2, "bit");
? ? ? ? map.put(3, "hello");
? ? ? ? map.put(4, "lambda");
? ? ? ? map.forEach((Integer integer, String s) -> {
? ? ? ? ? ? ? ? ? ? System.out.println(integer + " " + s);
? ? ? ? ? ? ? ? }
? ? ? ? );
? ? }
}還可以對代碼繼續(xù)簡化:
public class TestDemo2 {
? ? public static void main(String[] args) {
? ? ? ? HashMap<Integer, String> map = new HashMap<>();
? ? ? ? map.put(1, "hello");
? ? ? ? map.put(2, "bit");
? ? ? ? map.put(3, "hello");
? ? ? ? map.put(4, "lambda");
? ? ? ? map.forEach((integer, s) ->
? ? ? ? ? ? ? ? System.out.println(integer + " " + s)
? ? ? ? );
? ? }
}總結(jié):
Lambda表達(dá)式的優(yōu)點(diǎn)很明顯,在代碼層次上來說,使代碼變得非常的簡潔。缺點(diǎn)也很明顯,代碼不易讀。
優(yōu)點(diǎn):
代碼簡潔,開發(fā)迅速
方便函數(shù)式編程
非常容易進(jìn)行并行計(jì)算
Java 引入 Lambda,改善了集合操作
缺點(diǎn):
代碼可讀性變差
在非并行計(jì)算中,很多計(jì)算未必有傳統(tǒng)的 for 性能要高
不容易進(jìn)行調(diào)試
相關(guān)文章
Java實(shí)現(xiàn)大數(shù)運(yùn)算的實(shí)例代碼
這篇文章主要介紹了Java實(shí)現(xiàn)大數(shù)運(yùn)算的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
如何使用axis調(diào)用WebService及Java?WebService調(diào)用工具類
Axis是一個(gè)基于Java的Web服務(wù)框架,可以用來調(diào)用Web服務(wù)接口,下面這篇文章主要給大家介紹了關(guān)于如何使用axis調(diào)用WebService及Java?WebService調(diào)用工具類的相關(guān)資料,需要的朋友可以參考下2023-04-04
SpringCloud持久層框架MyBatis Plus的使用與原理解析
MyBatisPlus為MyBatis的增強(qiáng)版,專注于簡化數(shù)據(jù)庫操作,提供自動化CRUD、內(nèi)置分頁和樂觀鎖等功能,極大提升開發(fā)效率,在SpringCloud微服務(wù)架構(gòu)中,MyBatisPlus通過插件機(jī)制和自動生成代碼功能,有效支持企業(yè)級應(yīng)用和分布式系統(tǒng)的開發(fā)2024-10-10
關(guān)于解決雪花算法生成的ID傳輸前端后精度丟失問題
這篇文章主要介紹了關(guān)于解決雪花算法生成的ID傳輸前端后精度丟失問題,雪花算法生成的ID傳輸?shù)角岸藭r(shí),會出現(xiàn)后三位精度丟失,本文提供了解決思路,需要的朋友可以參考下2023-03-03
SpringMVC加載控制與Postmand的使用和Rest風(fēng)格的引入及RestFul開發(fā)全面詳解
SpringMVC是一種基于Java,實(shí)現(xiàn)了Web MVC設(shè)計(jì)模式,請求驅(qū)動類型的輕量級Web框架,即使用了MVC架構(gòu)模式的思想,將Web層進(jìn)行職責(zé)解耦?;谡埱篁?qū)動指的就是使用請求-響應(yīng)模型,框架的目的就是幫助我們簡化開發(fā),SpringMVC也是要簡化我們?nèi)粘eb開發(fā)2022-10-10

