• 【Java系列】JDK 1.8 新特性之 Lambda表达式


    在这里插入图片描述


    在这里插入图片描述


    1、Lambda表达式介绍

    Lambda是一个匿名函数,我们可以将Lambda表达式理解为一段可以传递的代码(将代码像数据一样传递)。使用它可以写出简洁、灵活的代码。作为一种更紧凑的代码风格,使java语言表达能力得到提升。

    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();
            
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    结果:

    ​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体

    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();
        }
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    22:22:39.125 [main] INFO com.chen.test.JAVA8Features.Demo02 - Lambda表达式:简洁、灵活,优雅永不过时

    Process finished with exit code 0

    语法格式二: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("接受爱不一定爱对方,爱一定付出真心爱");
        }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    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只有一个参数时,可以省略()

    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只有一个参数时,可以省略()");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    23:08:27.295 [main] INFO com.chen.test.JAVA8Features.Demo04 - Lambda只有一个参数时,可以省略()

    Process finished with exit code 0

    语法格式四: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));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    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和{}可以省略掉

    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));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    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

    语法格式六:类型推断:数据类型可以省略,因为编译器可以推断得出,成为“类型推断”

    
    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 !");
        }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    系列文章


    内容地址 链接
    JAVA面试常见问题
    JAVA面试Spring知识点
    JAVA面试Mysql
    JAVA面试Redis常见问题
    JAVA面试MongoDB
    JAVA介绍Linux (实战)常用命令
    =========================================================================
    👊如果你对该系列文章有兴趣的话,欢迎持续关注博主动态,博主会持续输出优质内容👊

    👊 博主很需要大家的支持,你的支持是我创作的不竭动力👊

    👊 ~ 点赞收藏+关注 ~👊
    =========================================================================

    版本记录


    • 2023-10-18 第一版
  • 相关阅读:
    2023年Q3乳品行业数据分析(乳品市场未来发展趋势)
    servlet实现登录功能【当用户当前未登陆,跳转登录页面才能访问,若已经登录了,才可以直接访问】
    echarts案例之日历
    Springcloud之Nacos Config服务配置
    第十三章《集合》第3节:Set集合
    Css3使用
    服务端主动关闭连接,如何确保对端能够收到全部的数据?
    【网页设计】期末大作业html+css(音乐网站)
    关于 SAP UI5 控件内容的 Excel 导出功能,如何加载所需的导出工具库
    RabbitMQ高可用集群部署
  • 原文地址:https://blog.csdn.net/qq_38517630/article/details/136215899