Lambda 四大内置核心函数式接口

一、四大核心内置函数

之前每当我们在使用 Lambda 表达式的时候都需要定义一个函数型的接口,这样就显得很麻烦

考虑到这些情况,Java 8 便帮我们内置了一些函数型接口,其中最常见的就是下面这四大核心函数型接口 Cousumer<T>、Supplier<T>、Function<T R>、Predicate<T>

二、测试四大函数型接口

public class FilterApple {
    // 消费性接口 Consumer
    public static void filterConsumer(List<Apple> apples, Consumer consumer) {
        consumer.accept(apples);
    }

    // 供给型接口 Supplier
    public static List<Apple> filterSupplier(List<Apple> apples, Supplier<List<Apple>> supplier) {
        return supplier.get();
    }

    // 函数型接口 Function
    public static List<Apple> filterFunction(List<Apple> apples,Function<String,List<Apple>> function){
        List<Apple> greenApple = function.apply("green");
        return greenApple;
    }

    // 断言型接口 Predicate
    public static Boolean filterPredicate(List<Apple> apples, Predicate<String> predicate){
        return predicate.test("yellow");
    }

    public static void main(String[] args) {
        // 初始化数据
        List<Apple> apples = Arrays.asList(
                new Apple(1, "red", 100.00),
                new Apple(2, "green", 200.00),
                new Apple(3, "yellow", 300.00)
        );

        // 测试消费性接口 Consumer
        filterConsumer(apples, (x) -> System.out.println("hello consumer function"));

        // 测试供给型接口 Supplier
        List<Apple> red = filterSupplier(apples, () -> {
            List<Apple> appleList = new ArrayList<>();

            if (!CollectionUtils.isEmpty(apples)) {
                for (Apple apple : apples) {
                    if (Objects.equals(apple.getColor(), "red")) {
                        appleList.add(apple);
                    }

                }
            }
            return appleList;
        });
        System.out.println(red);

        // 测试函数型接口 Function
        List<Apple> green = filterFunction(apples, (x) -> {
            List<Apple> appleList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(apples)) {
                for (Apple apple : apples) {
                    if (Objects.equals(apple.getColor(), x)) {
                        appleList.add(apple);
                    }

                }
            }
            return appleList;
        });
        System.out.println(green);

        // 测试断言型接口 Predicate
        Boolean hasYellow = filterPredicate(apples, (x) -> {
            boolean flag = false;
            if (!CollectionUtils.isEmpty(apples)) {
                for (Apple apple : apples) {
                    if (Objects.equals(apple.getColor(), x)) {
                        flag = true;
                    }
                }
            }
            return flag;
        });
        System.out.println(hasYellow);
    }
}

三、测试结果

四、总结

编号 接口 实现方法 描述
1  Consumer<T> void accept(T t)  消费性接口 Consumer, 接收一个 T 类型的参数,无返回值
2  Supplier<T> T get() 供给型接口 Supplier, 无参数,返回一个 T 类型的返回值
3  Function<T R> R apply(T t)  函数型接口 Function, 接收一个 T 类型的参数,返回一个 R 类型的返回值 
4  Predicate<T t> boolean test(T t) 断言型接口 Predicate, 接收一个 T 类型的参数,返回一个 boolean 类型的返回值
原文地址:https://www.cnblogs.com/xiaomaomao/p/15449324.html