目录
1、Lambda表达式介绍
2、从匿名类到Lambda转换
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();
}
}
3、Lambda表达式 六种语法格式
语法格式一:无参数、无返回值,只需要一个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表达式:简洁、灵活,优雅永不过时");
t1.run();
}
}
语法格式二:lambda有一个参数、无返回值
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("接受爱不一定爱对方,爱一定付出真心爱");
}
}
语法格式三:Lambda只有一个参数时,可以省略()
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只有一个参数时,可以省略()");
}
}
语法格式四:Lambda有两个参数时,并且有返回值
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));
}
}
语法格式五:当Lambda体只有一条语句的时候,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表达式
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));
}
}
语法格式六:类型推断:数据类型可以省略,因为编译器可以推断得出,成为“类型推断”
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 !");
}
}
系列文章
版本记录
- 2023-10-18 第一版