
1、Lambda表達(dá)式介紹
Lambda是一個(gè)匿名函數(shù),我們可以將Lambda表達(dá)式理解為一段可以傳遞的代碼(將代碼像數(shù)據(jù)一樣傳遞)。使用它可以寫出簡(jiǎn)潔、靈活的代碼。作為一種更緊湊的代碼風(fēng)格,使java語(yǔ)言表達(dá)能力得到提升。
2、從匿名類到Lambda轉(zhuǎn)換
package com.chen.test.JAVA8Features;
?
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
?
public class Demo01 {
private static Logger log = LoggerFactory.getLogger(Demo01.class);
?
public static void main(String[] args) {
Runnable t1 =new Runnable(){
@Override
public void run(){
log.info("我是沒(méi)有使用Lambda表達(dá)式:不簡(jiǎn)潔");
}
};
Runnable t2 = () -> log.info("我是使用Lambda表達(dá)式:簡(jiǎn)潔、靈活");
t1.run();
t2.run();
}
}
結(jié)果:
?19:43:39.303 [main] INFO com.chen.test.JAVA8Features.Demo01 - 我是沒(méi)有使用Lambda表達(dá)式:不簡(jiǎn)潔、代碼多
19:43:39.303 [main] INFO com.chen.test.JAVA8Features.Demo01 - 我是使用Lambda表達(dá)式:簡(jiǎn)潔、靈活
3、Lambda表達(dá)式 六種語(yǔ)法格式
Lambda表達(dá)式在java語(yǔ)言中引入了一種新的語(yǔ)法元素和操作。
這種操作符號(hào)為“->”,
Lambda操作符或箭頭操作符,它將Lambda表達(dá)式分割為兩部分。
左邊:指Lambda表達(dá)式的所有參數(shù)
右邊:指Lambda體,即表示Lambda表達(dá)式需要執(zhí)行的功能。
語(yǔ)法格式一:無(wú)參數(shù)、無(wú)返回值,只需要一個(gè)Lambda體
package com.chen.test.JAVA8Features;
?
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
?
public class Demo02 {
private static Logger log = LoggerFactory.getLogger(Demo02.class);
?
public static void main(String[] args) {
Runnable t1 = ()-> log.info("Lambda表達(dá)式:簡(jiǎn)潔、靈活,優(yōu)雅永不過(guò)時(shí)");
t1.run();
}
}
22:22:39.125 [main] INFO com.chen.test.JAVA8Features.Demo02 - Lambda表達(dá)式:簡(jiǎn)潔、靈活,優(yōu)雅永不過(guò)時(shí)
?
Process finished with exit code 0
語(yǔ)法格式二:lambda有一個(gè)參數(shù)、無(wú)返回值
package com.chen.test.JAVA8Features;
?
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
?
import java.util.function.Consumer;
?
public class Demo03 {
private static Logger log = LoggerFactory.getLogger(Demo03.class);
public static void main(String[] args) {
Consumer<String> consumer = new Consumer<String>() {
@Override
public void accept(String s) {
log.info(s);
}
};
consumer.accept("愛(ài)與被愛(ài)的區(qū)別");
?
Consumer<String> consumer1 = (s) -> log.info(s);
consumer1.accept("接受愛(ài)不一定愛(ài)對(duì)方,愛(ài)一定付出真心愛(ài)");
}
}
?
23:03:08.992 [main] INFO com.chen.test.JAVA8Features.Demo03 - 愛(ài)與被愛(ài)的區(qū)別
23:03:09.142 [main] INFO com.chen.test.JAVA8Features.Demo03 - 接受愛(ài)不一定愛(ài)對(duì)方,愛(ài)一定付出真心愛(ài)
?
Process finished with exit code 0
?語(yǔ)法格式三:Lambda只有一個(gè)參數(shù)時(shí),可以省略()
package com.chen.test.JAVA8Features;
?
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
?
import java.util.function.Consumer;
?
public class Demo04 {
private static Logger log = LoggerFactory.getLogger(Demo04.class);
public static void main(String[] args) {
Consumer<String> consumer = s -> log.info(s);
consumer.accept("Lambda只有一個(gè)參數(shù)時(shí),可以省略()");
}
}
?
23:08:27.295 [main] INFO com.chen.test.JAVA8Features.Demo04 - Lambda只有一個(gè)參數(shù)時(shí),可以省略()
?
Process finished with exit code 0
語(yǔ)法格式四:Lambda有兩個(gè)參數(shù)時(shí),并且有返回值
package com.chen.test.JAVA8Features;
?
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
?
import java.util.Comparator;
?
?
public class Demo05 {
private static Logger log = LoggerFactory.getLogger(Demo05.class);
?
public static void main(String[] args) {
CompareOldMethod(12,10);
findMaxValue(12,10);
findMinValue(12,10);
}
// 沒(méi)有使用Lambda表達(dá)式比較大小
public static void CompareOldMethod(int num1,int num2){
Comparator<Integer> comparator = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
log.info("o1:{}",o1);
log.info("o2:{}",o2);
return o1 < o2 ? o2 : o1;
}
};
log.info("OldFindMaxValue:{}",comparator.compare(num1,num2));
}
?
// 使用lambda表達(dá)式
public static void findMaxValue(int num1,int num2){
Comparator<Integer> comparatorMax = (o1, o2) ->{
?
log.info("o1:{}",o1);
log.info("o2:{}",o2);
return (o1<o2)? o2 :(o1);
};
?
log.info("findMaxValue:{}",(comparatorMax.compare(num1,num2)));
?
}
public static void findMinValue(int num1,int num2){
Comparator<Integer> comparatorMin = (o1, o2) -> {
log.info("o1:{}",o1);
log.info("o2:{}",o2);
return (o1 < o2) ? o1 : o2;
};
log.info("FindMinValue:{}",comparatorMin.compare(num1,num2));
}
}
?
00:17:10.206 [main] INFO com.chen.test.JAVA8Features.Demo05 - o1:12
00:17:10.206 [main] INFO com.chen.test.JAVA8Features.Demo05 - o2:10
00:17:10.206 [main] INFO com.chen.test.JAVA8Features.Demo05 - OldFindMaxValue:12
00:17:10.315 [main] INFO com.chen.test.JAVA8Features.Demo05 - o1:12
00:17:10.315 [main] INFO com.chen.test.JAVA8Features.Demo05 - o2:10
00:17:10.315 [main] INFO com.chen.test.JAVA8Features.Demo05 - findMaxValue:12
00:17:10.315 [main] INFO com.chen.test.JAVA8Features.Demo05 - o1:12
00:17:10.315 [main] INFO com.chen.test.JAVA8Features.Demo05 - o2:10
00:17:10.315 [main] INFO com.chen.test.JAVA8Features.Demo05 - FindMinValue:10
?
Process finished with exit code 0
?
語(yǔ)法格式五:當(dāng)Lambda體只有一條語(yǔ)句的時(shí)候,return和{}可以省略掉
package com.chen.test.JAVA8Features;
?
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
?
import java.util.Comparator;
?
?
public class Demo05 {
private static Logger log = LoggerFactory.getLogger(Demo05.class);
?
public static void main(String[] args) {
findMaxValue(12,10);
findMinValue(12,10);
}
?
// 使用lambda表達(dá)式
public static void findMaxValue(int num1,int num2){
Comparator<Integer> comparatorMax = (o1, o2) ->{
?
log.info("o1:{}",o1);
log.info("o2:{}",o2);
return (o1<o2)? o2 :(o1);
};
?
log.info("findMaxValue:{}",(comparatorMax.compare(num1,num2)));
?
}
public static void findMinValue(int num1,int num2){
Comparator<Integer> comparatorMin = (o1, o2) -> (o1 < o2) ? o1 : o2;
?
log.info("FindMinValue:{}",comparatorMin.compare(num1,num2));
}
}
?
00:22:31.059 [main] INFO com.chen.test.JAVA8Features.Demo05 - o1:12
00:22:31.075 [main] INFO com.chen.test.JAVA8Features.Demo05 - o2:10
00:22:31.075 [main] INFO com.chen.test.JAVA8Features.Demo05 - findMaxValue:12
00:22:31.075 [main] INFO com.chen.test.JAVA8Features.Demo05 - FindMinValue:10
?
Process finished with exit code 0
語(yǔ)法格式六:類型推斷:數(shù)據(jù)類型可以省略,因?yàn)榫幾g器可以推斷得出,成為“類型推斷”
package com.chen.test.JAVA8Features;
?
import com.mysql.cj.callback.MysqlCallbackHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
?
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
?
?
public class Demo07 {
private static Logger log = LoggerFactory.getLogger(Demo07.class);
?
public static void main(String[] args) {
dateType();
}
?
public static void dateType(){
Consumer<String> consumer = (String s) -> log.info(s);
consumer.accept("Hello World !");
?
Consumer<String> consumer1 = (s) -> log.info(s);
consumer1.accept("Hello don't date type !");
}
}
?
系列文章
內(nèi)容 | 地址 鏈接 |
---|---|
JAVA面試 | 常見(jiàn)問(wèn)題 |
JAVA面試 | Spring知識(shí)點(diǎn) |
JAVA面試 | Mysql |
JAVA面試 | Redis常見(jiàn)問(wèn)題 |
JAVA面試 | MongoDB |
JAVA介紹 | Linux (實(shí)戰(zhàn))常用命令 |
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-836247.html文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-836247.html
?
版本記錄
- 2023-10-18 第一版
到了這里,關(guān)于【Java系列】JDK 1.8 新特性之 Lambda表達(dá)式的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!