1-lambda表达式

简介

Java8引入函数式编程风格

可以理解为一种匿名函数的代替

通过行为参数化传递代码

形式

①(parameters) -> expression

②(parameters)  -> {statement;}

形式一:没有参数

() -> System.out.println("lambda demo1");

形式二:只有一个参数

(param) -> System.out.println("lambda demo2 " + param);

形式三:没有参数,逻辑复杂

() -> {
    System.out.println("lambda demo3 ");
    System.out.println("lambda demo3 ");
}

形式四:包含两个参数的方法

BinaryOperator<Long> functionAdd = (x, y) -> x + y;
Long res = functionAdd.apply(1L, 2L);

形式五:对参数显示声明

BinaryOperator<Long> functionAdd = (Long x, Long y) -> x + y;
Long res = functionAdd.apply(1L, 2L);

函数式接口

接口只能有一个抽象方法

/**
 * @description: Sku选择谓词接口
 */
public interface SkuPredicate {

    /**
     * 选择判断标准
     * @param sku
     * @return
     */
    boolean test(Sku sku);
}

Java8的函数式接口注解: @FunctionalInterface

/**
 * 
 * @FunctionalInterface 会校验该接口是否只有一个抽象方法
 * @description: 文件处理函数式接口
 */
@FunctionalInterface
public interface FileConsumer {

    /**
     * 函数式接口抽象方法
     * @param fileContent
     */
    void fileHandler(String fileContent);
}

函数式接口的抽象方法前面:函数描述符

posted @ 2020-06-05 11:01  mingmingn  阅读(194)  评论(0)    收藏  举报