Java函数式接口

一.初识函数式接口

  • 只包含一个抽象方法的接口,称为函数式接口。
  • 你可以通过 Lambda 表达式来创建该接口的对象。若 Lambda 表达式抛出一个受检异常(即:非运行时异常),那么该异常需要在目标接口的抽象方法上进行声明。
  • 我们可以在一个接口上使用 @FunctionalInterface 注解,这样做可以检查它是否是一个函数式接口。同时 javadoc 也会包含一条声明,说明这个接口是一个函数式接口。
  • 在java.util.function包下定义了Java 8 的丰富的函数式接口

二.Java内置四大核心函数式接口

函数式接口参数类型返回类型用途
Consumer <T> 消费型接口Tvoid对类型为T的对象应用操作,包含方法:void accept(T t)
Supplier <T> 供给型接口T返回类型为T的对象,包含方法:T get()
Function<T,R> 函数型接口TR对类型为T的对象应用操作,并返回结果。结 果是R类型的对象。包含方法:R apply(T t)
Predicate<T> 断定型接口Tboolean确定类型为T的对象是否满足某约束,并返回 boolean 值。包含方法:boolean test(T t)

举例:
代码示例一:

    @Test
    public void test1(){
        //原始写法
        Consumer<Integer> consumer = new Consumer<>() {
            @Override
            public void accept(Integer i) {
                System.out.println("买物品需:" + i + "元");
            }
        };

        buySomething(999,consumer);

        //匿名实现类的匿名对象写法
        buySomething(555,new Consumer<Integer>(){

            @Override
            public void accept(Integer integer) {
                System.out.println("买球鞋需:" + integer + "元");
            }
        });

        //Lambda表达式写法
        buySomething(666,i -> System.out.println("买乳清蛋白粉需:" + i + "元"));
        
    }

    public void buySomething(int money, Consumer<Integer> consumer){
        consumer.accept(money);
    }

输出结果:

买物品需:999元
买球鞋需:555元
买乳清蛋白粉需:666元

Process finished with exit code 0

代码示例二:

    @Test
    public void test2(){
        List<Integer> list = Arrays.asList(123,456,789,555,999,2,333,6,88,99,77,66,33,233,888,111);

        List<Integer> getList = meetTheConditions(list,i -> i <= 333);

        System.out.println(getList);
    }

    public List<Integer> meetTheConditions(List<Integer> list,Predicate<Integer> predicate){
        ArrayList<Integer> arrayList = new ArrayList<>();

        for (Integer i : list) {
            if(predicate.test(i)){
                arrayList.add(i);
            }
        }

        return arrayList;
    }

输出结果:

[123, 2, 333, 6, 88, 99, 77, 66, 33, 233, 111]

Process finished with exit code 0

三.其他接口

函数式接口参数类型返回类型用途
BiFunction<T, U, R>T, UR对类型为 T, U 参数应用操作,返回 R 类型的结 果。包含方法为: R apply(T t, U u);
UnaryOperator<T> (Function子接口)TT对类型为T的对象进行一元运算,并返回T类型的 结果。包含方法为:T apply(T t);
BinaryOperator<T> (BiFunction 子接口)T, Uvoid对类型为T, U 参数应用操作。 包含方法为: void accept(T t, U u)
BiConsumer<T, U>T, Uvoid对类型为T, U 参数应用操作。 包含方法为: void accept(T t, U u)
BiPredicate<T,U>T,Uboolean包含方法为: boolean test(T t,U u)
ToIntFunction<T> ToLongFunction<T> ToDoubleFunction<T>Tint long double分别计算int、long、double值的函数
IntFunction<R> LongFunction<R> DoubleFunction<R>int long doubleR参数分别为int、long、double 类型的函数
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MrYuShiwen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值