目录
- 1、Lambda表达式介绍
- 2、从匿名类到Lambda转换
- [3、Lambda表达式 六种语法格式](#3、Lambda表达式 六种语法格式)
- 系列文章
- 版本记录
1、Lambda表达式介绍
Lambda是一个匿名函数,我们可以将Lambda表达式理解为一段可以传递的代码(将代码像数据一样传递)。使用它可以写出简洁、灵活的代码。作为一种更紧凑的代码风格,使java语言表达能力得到提升。
2、从匿名类到Lambda转换
java
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("我是没有使用Lambda表达式:不简洁");
}
};
Runnable t2 = () -> log.info("我是使用Lambda表达式:简洁、灵活");
t1.run();
t2.run();
}
}
结果:
19:43:39.303 [main] INFO com.chen.test.JAVA8Features.Demo01 - 我是没有使用Lambda表达式:不简洁、代码多19:43:39.303 [main] INFO com.chen.test.JAVA8Features.Demo01 - 我是使用Lambda表达式:简洁、灵活
3、Lambda表达式 六种语法格式
Lambda表达式在java语言中引入了一种新的语法元素和操作。
这种操作符号为"->",
Lambda操作符或箭头操作符,它将Lambda表达式分割为两部分。
左边:指Lambda表达式的所有参数
右边:指Lambda体,即表示Lambda表达式需要执行的功能。
语法格式一:无参数、无返回值,只需要一个Lambda体
java
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表达式:简洁、灵活,优雅永不过时");
t1.run();
}
}
22:22:39.125 [main] INFO com.chen.test.JAVA8Features.Demo02 - Lambda表达式:简洁、灵活,优雅永不过时
Process finished with exit code 0
语法格式二:lambda有一个参数、无返回值
java
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("爱与被爱的区别");
Consumer<String> consumer1 = (s) -> log.info(s);
consumer1.accept("接受爱不一定爱对方,爱一定付出真心爱");
}
}
23:03:08.992 [main] INFO com.chen.test.JAVA8Features.Demo03 - 爱与被爱的区别
23:03:09.142 [main] INFO com.chen.test.JAVA8Features.Demo03 - 接受爱不一定爱对方,爱一定付出真心爱
Process finished with exit code 0
语法格式三:Lambda只有一个参数时,可以省略()
java
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只有一个参数时,可以省略()");
}
}
23:08:27.295 [main] INFO com.chen.test.JAVA8Features.Demo04 - Lambda只有一个参数时,可以省略()
Process finished with exit code 0
语法格式四:Lambda有两个参数时,并且有返回值
java
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);
}
// 没有使用Lambda表达式比较大小
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表达式
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
语法格式五:当Lambda体只有一条语句的时候,return和{}可以省略掉
java
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表达式
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
语法格式六:类型推断:数据类型可以省略,因为编译器可以推断得出,成为"类型推断"
java
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 !");
}
}
系列文章
内容 | 地址 链接 |
---|---|
JAVA面试 | 常见问题 |
JAVA面试 | Spring知识点 |
JAVA面试 | Mysql |
JAVA面试 | Redis常见问题 |
JAVA面试 | MongoDB |
JAVA介绍 | Linux (实战)常用命令 |
========================================================================= 👊如果你对该系列文章有兴趣的话,欢迎持续关注博主动态,博主会持续输出优质内容👊
👊 博主很需要大家的支持,你的支持是我创作的不竭动力👊
👊 **~ 点赞收藏+关注 ~**👊 =========================================================================
版本记录
- 2023-10-18 第一版