zoukankan      html  css  js  c++  java
  • 详解 Lambda表达式

    Lambda表达式

    概述

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

    首先,本人要声明的一点是:

    Lambda表达式仅适用于 函数式接口
    (函数式接口:只含有一个抽象方法的接口)

    那么,现在,本人就通过对于例子的简化,来讲解Lambda表达式的语法要求:

    首先,本人来给出一个接口:

    package edu.youzg.about_new_features.core.about_jdk8.core;
    
    public interface MyInterface {
        Object add(int a,int b);
    }
    

    那么,我们以往想要在使用这个接口的话,就要按如下的格式:

    package edu.youzg.about_new_features.core.about_jdk8.core;
    
    public class Test {
    
        public static void main(String[] args) {
            MyInterface myInterface = new MyInterface() {
    
                @Override
                public Object add(int a, int b) {
                    return a + b;
                }
            };
    
            System.out.println(myInterface.add(10, 20));
        }
    
    }
    

    想必结果大家肯定都知道:
    在这里插入图片描述

    那么,在我们引入Lambda表达式的知识后,就可以化简为如下格式:

    package edu.youzg.about_new_features.core.about_jdk8.core;
    
    public class Test {
    
        public static void main(String[] args) {
            //原始写法
            MyInterface myInterface = new MyInterface() {
    
                @Override
                public Object add(int a, int b) {
                    return a + b;
                }
            };
            System.out.println(myInterface.add(10, 20));
            
            //Lambda表达式 * 一阶
            MyInterface myInterface1 = (int a, int b) -> {
                return a + b;
            };
            System.out.println(myInterface1.add(10, 20 ));
        }
    
    }
    

    那么,我们来看一下运行结果:
    在这里插入图片描述
    其实,参数类型也可以省略不写:

    package edu.youzg.about_new_features.core.about_jdk8.core;
    
    public class Test {
    
        public static void main(String[] args) {
            //原始写法
            MyInterface myInterface = new MyInterface() {
    
                @Override
                public Object add(int a, int b) {
                    return a + b;
                }
            };
            System.out.println(myInterface.add(10, 20));
            
            //Lambda表达式 * 一阶
            MyInterface myInterface1 = (int a, int b) -> {
                return a + b;
            };
            System.out.println(myInterface1.add(10, 20 ));
    
            //Lambda表达式 * 二阶
            MyInterface myInterface2 = (a, b) -> {
                return a + b;
            };
            System.out.println(myInterface2.add(10, 20 ));
        }
    
    }
    

    在这里插入图片描述
    令人惊叹的是:由于实现这个接口,我们只写了一行return,所以,源代码快还可以省去return和{} :

    package edu.youzg.about_new_features.core.about_jdk8.core;
    
    public class Test {
    
        public static void main(String[] args) {
            //原始写法
            MyInterface myInterface = new MyInterface() {
    
                @Override
                public Object add(int a, int b) {
                    return a + b;
                }
            };
            System.out.println(myInterface.add(10, 20));
            
            //Lambda表达式 * 一阶
            MyInterface myInterface1 = (int a, int b) -> {
                return a + b;
            };
            System.out.println(myInterface1.add(10, 20 ));
    
            //Lambda表达式 * 二阶
            MyInterface myInterface2 = (a, b) -> {
                return a + b;
            };
            System.out.println(myInterface2.add(10, 20 ));
    
            //Lambda表达式 * 三阶
            MyInterface myInterface3 = (a, b) -> a + b;
            System.out.println(myInterface3.add(10, 20 ));
        }
    
    }
    

    那么,本人来展示下运行结果
    在这里插入图片描述


    现在,本人来给出一个Model类:

    package edu.youzg.about_new_features.core.about_jdk8.core;
    
    public class Model {
        int value;
        int number;
    
        public Model(int value, int number) {
            System.out.println("执行了双参构造");
            this.value = value;
            this.number = number;
        }
    
        public int getValue() {
            return value;
        }
    
        public int getNumber() {
            return number;
        }
    
    }
    

    现在,若是本人想在该函数接口的抽象方法中返回一个刚申请对象的成员的值:

    package edu.youzg.about_new_features.core.about_jdk8.core;
    
    public class Test {
    
        public static void main(String[] args) {
            //原始写法
            MyInterface myInterface = new MyInterface() {
                @Override
                public Object add(int a, int b) {
                    Model model = new Model(a, b);
                    return model;
                }
            };
            Model add = (Model) myInterface.add(1, 2);
            System.out.println("value为:" + add.getValue() + "
    number为:" + add.number);
        }
    
    }
    

    那么,我们可以转换为如下格式:

    package edu.youzg.about_new_features.core.about_jdk8.core;
    
    public class Test {
    
        public static void main(String[] args) {
            //原始写法
            MyInterface myInterface = new MyInterface() {
                @Override
                public Object add(int a, int b) {
                    Model model = new Model(a, b);
                    return model;
                }
            };
            Model add = (Model) myInterface.add(1, 2);
            System.out.println("value为:" + add.getValue() + "
    number为:" + add.number);
    
            System.out.println("===================================");
            //Lambda表达式 版本
            MyInterface myInterface1 =  Model::new;
            Model add1 = (Model) myInterface.add(1, 2);
            System.out.println("value为:" + add1.getValue() + "
    number为:" + add1.number);
        }
    
    }
    

    现在,本人来展示下运行结果:
    在这里插入图片描述

    那么,对于这个转换,本人再来给出个例子:

    package edu.youzg.about_new_features.core.about_jdk8.core;
    
    public class Test {
    
        public static void main(String[] args) {
            //原始写法
            MyInterface myInterface = new MyInterface() {
                @Override
                public Object add(int a, int b) {
                    int min = Math.min(a, b);
                    return min;
                }
            };
            System.out.println(myInterface.add(10, 20));
    
            System.out.println("===================================");
            //Lambda表达式 版本
            MyInterface myInterface1 =  Math::min;
            System.out.println(myInterface1.add(10, 20));
        }
    
    }
    

    那么,本人再来展示下运行结果:
    在这里插入图片描述


    上面两个例子分别是 构造方法、静态方法的返回,
    那么,现在,本人再来展示最后一种可转换条件 —— 非静态方法:

    package edu.youzg.about_new_features.core.about_jdk8.core;
    
    public class Test {
    
        public static void main(String[] args) {
            //原始写法
            MyInterface myInterface = new MyInterface() {
                @Override
                public Object add(int a, int b) {
                    Model model = new Model(a, b);
                    return model.plus(10, 20);
                }
            };
            System.out.println(myInterface.add(10, 20));
    
            System.out.println("===================================");
            //Lambda表达式 版本
            Model model = new Model(5, 1);
            MyInterface myInterface1 =  model::plus;
            System.out.println(myInterface1.add(10, 20));
        }
    
    }
    

    现在,本人来展示下运行结果:
    在这里插入图片描述

    那么,有关Lambda表达式的所有可能出现的例子在上面就展示完毕了。
    那么,现在,本人来稍作总结:

    操作符为 “ ->” , 该操作符被称为 Lambda 操作符或箭头操作符。
    它将 Lambda 分为两个部分:
    左侧: 指定了 Lambda 表达式需要的所有参数
    右侧: 指定了 Lambda 体,即 Lambda 表达式要执行的功能


    方法引用:使用操作符 “ ::” 将方法名和对象或类的名字分隔开来
    对象::实例方法
    类::静态方法
    类::实例方法


  • 相关阅读:
    Linux企业级项目实践之网络爬虫(6)——将程序设计成为守护进程
    Linux企业级项目实践之网络爬虫(5)——处理配置文件
    Linux企业级项目实践之网络爬虫(3)——设计自己的网络爬虫
    Linux企业级项目实践之网络爬虫(4)——主程序流程
    Linux企业级项目实践之网络爬虫(1)——项目概述及准备工作
    Linux企业级项目实践之网络爬虫(2)——网络爬虫的结构与工作流程
    泛化、依赖、关联、聚合、组合
    日常(停课后的月考)
    日常(停课后的月考)
    打击罪犯
  • 原文地址:https://www.cnblogs.com/codderYouzg/p/12419144.html
Copyright © 2011-2022 走看看