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

    1、Lambda 表达式

    我们可以看下面这一段代码,然后引出 Lambda 表达式

    1.1、首先定义一个接口

    public interface MyInterface {
        public String getMessage();
    }
    

    1.2、然后再定义一个方法,并测试

    public class NewFeature {
        public static void main(String[] args) {
            String message = NewFeature.getMessage(new MyInterface() {
                @Override
                public String getMessage() {
                    return "hello world";
                }
            });
            System.out.println(message);
        }
    
        public static String getMessage(MyInterface myInterface){
            return myInterface.getMessage();
        }
    }
    

    从上面我们可以看出 main 方法中调用了 getMessage 方法,该方法中的参数是一个接口类型,那么我们实际使用的时候应该传递的是该接口的一个子实现类,所以我们很自然的就想到了使用 匿名内部类的方式进行参数的传递,但是 匿名内部类的方式代码比较多,我们可以有更加优雅的方式,也就是使用 Lambda 表达式.

    public class NewFeature {
        public static void main(String[] args) {
            String message = NewFeature.getMessage(() -> "hello world");
            System.out.println(message);
        }
    
        public static String getMessage(MyInterface myInterface){
            return myInterface.getMessage();
        }
    }
    

    使用了 Lambda 表达式之后,一行代码直接搞定,是不是看起来更加优雅了呢?

    这个东西就是 Java 8 新特性的 Lambda 表达式

    (): 里面放置 Lambda 表达式的参数

    ->: Lambda 表达式的箭头操作符、也叫 Lambda 操作符

    "hello world" : Lambda 操作体,如果只有一行语句,那么不需要 { } 进行包裹,但是如果 Lambda 体中的语句不止一条,则必须要加上 { }

    () -> "hello world"
    

      

    2、函数式接口

    在说 Lambda 表达式的时候我们就必须要提到另外一个概念,函数式接口,因为要想使用 Lambda 表达式需要函数式接口的支撑

    2.1、什么是函数式接口

    如果一个接口中只有一个方法,那么这个接口就是函数式接口,例如

    public interface MyInterface {
        public String getMessage();
    }
    

    当然也可以使用 @FunctionalInterface 注解进行限定,使用了该注解的好处就是一定能确保该接口是函数式接口,如果你在该接口中定义了两个方法,那么就会直接报错

    @FunctionalInterface
    public interface MyInterface {
        public String getMessage();
    }
    

      

    3、Lambda 表达式常见的几种形式

    3.1、方法无参数、无返回值

    @FunctionalInterface
    public interface MyInterface {
        // 方法无参数、无返回值
        public void getMessage();
    }
    
    public class NewFeature {
        public static void main(String[] args) {
            NewFeature.getMessage(()-> System.out.println("hello world"));
        }
    
        public static void getMessage(MyInterface myInterface){
            myInterface.getMessage();
        }
    }

    3.2、方法无参数、有返回值

    @FunctionalInterface
    public interface MyInterface {
        // 方法无参数、有返回值
        public String getMessage();
    }
    
    public class NewFeature {
        public static void main(String[] args) {
            String message = NewFeature.getMessage(() -> "hello lambda");
            System.out.println(message);
        }
    
        public static String getMessage(MyInterface myInterface){
            return myInterface.getMessage();
        }
    }

    3.3、方法有一个参数、无返回值

    @FunctionalInterface
    public interface MyInterface {
        // 方法有一个参数、无返回值
        public void getMessage(String userName);
    }
    
    public class NewFeature {
        public static void main(String[] args) {
            // 只有一个参数的情况下 () 可以省略不写
            // 等价于 NewFeature.getMessage(x -> System.out.println("hello " + x));
            NewFeature.getMessage((x) -> System.out.println("hello " + x));
        }
    
        public static void getMessage(MyInterface myInterface) {
            myInterface.getMessage("summer");
        }
    }

    3.4、方法有一个参数、有返回值

    @FunctionalInterface
    public interface MyInterface {
        // 方法有一个参数、有返回值
        public String getMessage(String userName);
    }
    
    public class NewFeature {
        public static void main(String[] args) {
            // 只有一个参数的情况下 () 可以省略不写
            // 等价于 String message = NewFeature.getMessage(x -> "hello " + x);
            String message = NewFeature.getMessage((x) -> "hello " + x);
            System.out.println(message);
        }
    
        public static String getMessage(MyInterface myInterface){
            return myInterface.getMessage("summer");
        }
    }

    3.5、方法有多个参数、无返回值

    @FunctionalInterface
    public interface MyInterface {
        // 方法有多个参数、无返回值
        public void getMessage(Integer id,String userName,Integer age);
    }
    
    public class NewFeature {
        public static void main(String[] args) {
            NewFeature.getMessage((x, y, z) -> System.out.println(y + " " + x + " " + z));
        }
    
        public static void getMessage(MyInterface myInterface) {
            myInterface.getMessage(9527, "summer", 28);
        }
    }

    3.6、方法有多个参数、有返回值

    @FunctionalInterface
    public interface MyInterface {
        // 方法有多个参数、有返回值
        public String getMessage(Integer id,String userName,Integer age);
    }
    
    public class NewFeature {
        public static void main(String[] args) {
            String message = NewFeature.getMessage((x, y, z) -> y + " " + x + " " + z);
            System.out.println(message);
        }
    
        public static String getMessage(MyInterface myInterface) {
            return myInterface.getMessage(9527, "summer", 28);
        }
    }

    3.7、方法有参数、Lambda 体有多条语句

    @FunctionalInterface
    public interface MyInterface {
        // 方法有多个参数、有返回值
        public String getMessage(Integer id,String userName,Integer age);
    }
    
    public class NewFeature {
        public static void main(String[] args) {
            String message = NewFeature.getMessage((x, y, z) -> {
                String str = "hello world";
                str = y + " " + x + " " + z + " " + str;
                return str;
            });
            System.out.println(message);
        }
    
        public static String getMessage(MyInterface myInterface) {
            return myInterface.getMessage(9527, "summer", 28);
        }
    }
    

      

    4、总结

    4.1、如果只有一个参数 () 可以省略不写

    4.2、如果 Lambda 体只有一条语句,那么 { } 可以省略不写,如果有返回值得情况下 return 也可以不写

    4.3、如果 Lambda 体中有多条语句,那么必须写上 { },如果有返回值的情况下, return 也必须写上

    4.4、最常用的 Lambda 表达式格式如下

    (x, y, z) -> {
        语句 1;
        语句 2;
        ........   
    }
    

      

  • 相关阅读:
    贝赛尔曲线实现填充不规则图形,并且随手指运动
    当view为wrap_conten时获取一个view的具体宽高
    Scrapped or attached views may not be recycled
    installation failed with message INSTALL_FAILED_INSUFFICIENT_STORG
    RecycleView设置顶部分割线(记录一个坑)
    Java list.remove( )方法需要注意的地方
    JAVA forname classnotfoundexception 错误
    调用android的getColor()方法出现 java.lang.NoSuchMethodError: android.content.res.Resources.getColor
    JSONObject.parseObject
    设置抓包工具Fiddler的host
  • 原文地址:https://www.cnblogs.com/xiaomaomao/p/14903764.html
Copyright © 2011-2022 走看看