• Java使用lamda表达式简化代码


    代码,自然写的越简洁越好啦,写的人舒服,看的人也舒服,一切为了高效。

    要把有限的时间花到其它有意思的事情上去。


     lamada表达式简化了匿名内部类


    函数接口

      只有一个抽象方法的接口,通常使用@FunctionalInterface注解标识。

    例如:

    Lambda

    格式:

      这等同于创建了一个匿名实现类对象。

    ()->{
      //方法体
    };

    例子

      匿名内部类创建线程

    复制代码
    public class LeaningLamda{
        public static void main(String[] args) {
            
            //需要借助父类或者接口来声明
            new Thread(new Runnable(){
                @Override
                public void run() {
                    System.out.println(this.getClass()+"我已经在跑了!");
                }
            }).start();
        }
    }
    复制代码

      使用lamda表达式实现

    复制代码
    public class LeaningLamda{
        public static void main(String[] args) {
            new Thread(()-> {
                    System.out.println("我已经在跑了!");
                }
            ).start();
        }
    }
    复制代码

    详细说明

    参数

      lamda表达式中的参数可以不指定类型,它会自适应。

    复制代码
    public class LeaningLamda2 {
        public static void main(String[] args) {
    
            MyInterface ls = (i,str)->{
                System.out.println("int:"+i);
                System.out.println("String:"+str);};
    
            ls.sayHi(520,"i love you!");
        }
    }
    interface MyInterface{
        void sayHi(int i,String str);
    }
    复制代码

    运行结果:

      只有一个参数时,括号可以省略

    复制代码
    public class LeaningLamda2 {
        public static void main(String[] args) {
    
            MyInterface ls = str-> {System.out.println("String:"+str);};
            ls.sayHi("i love you!");
        }
    }
    interface MyInterface{
        void sayHi(String str);
    }
    复制代码

    运行结果:

    返回值

      如果有返回值,正常返回

    复制代码
    public class LeaningLamda2 {
        public static void main(String[] args) {
    
            MyInterface ls = (String str)-> {
                String str2 = "最后的赢家是:"+str;
                return str2;};
            System.out.println(ls.sayHi("lurenjia"));
        }
    }
    interface MyInterface{
        String sayHi(String str);
    }
    复制代码

      如果方法体只有一条语句,大括号可以省略

    复制代码
    public class LeaningLamda2 {
        public static void main(String[] args) {
    
            MyInterface ls = (int i,String str)-> System.out.println("int:"+i+"----String:"+str);
            ls.sayHi(520,"i love you!");
        }
    }
    interface MyInterface{
        void sayHi(int i,String str);
    }
    复制代码

      如果方法体只有一条返回语句,return 可以省略

    复制代码
    public class LeaningLamda2 {
        public static void main(String[] args) {
    
            MyInterface ls = str-> "最后的赢家是:"+str;
            System.out.println(ls.sayHi("中国"));
        }
    }
    interface MyInterface{
        String sayHi(String str);
    }
    复制代码

    运行结果:


    总结

    1、接口为函数接口。

    2、需要创建匿名内部类。

    3、基本形式为:

    接口或父类   名称  = (参数类型1 参数1,参数类型2 参数2,...)->{
                内容
            };
    名称.方法(参数1,参数2,...);

    4、可以省略的是:

    1、参数类型可省,若只有一位,则括号可省。
    
    2、方法内容只有一条内容,大括号可省。
    
    3、内容只有一句return语句,return可省,直接写值。
  • 相关阅读:
    Ubuntu 安装Nacos
    PAT甲级真题1153: 解码PAT准考证
    最近面了12个人,发现这个测试基础题都答不上来...
    mac nvm的使用
    运维工程师现在用什么系统
    Java集合---List和Set
    Android 9.0 网络之netd详解
    vue el-form表单嵌套组件时正则校验不生效
    【面试题-Vue】常见问题二、组件类
    怎么预防鸡葡萄球菌病 防治鸡球菌病的特效药
  • 原文地址:https://www.cnblogs.com/lurenjia-bky/p/16876475.html