4-2 Java新特性玩转JDK8 函数式编程BiFunction

4-2 Java新特性玩转JDK8 函数式编程BiFunction


如果您还没有阅读本章须知,请先阅读➡ 本章须知


资料

  • BiFunction 为函数型接口,有两个参数,有返回值。对比Function 多了一个可传入的参数。
    @FunctionalInterface 接口用处是告诉其他团队成员该接口是一个函数式接口,有且只能有一个需实现的方法
@FunctionalInterface
public interface BiFunction<T, U, R> {
    R apply(T t, U u);
}

使用示例

两个参数的四则运算

public class Main {
    public static void main(String[] args) {
        System.out.println(operator(10,21,(a,b)->a+b));
        System.out.println(operator(10,2,(a,b)->a-b));
        System.out.println(operator(8,4,(a,b)->a*b));
        System.out.println(operator(10,2,(a,b)->a/b));
    }
	// BiFunction<参数一类型,参数二类型,返回值类型>
    public static Integer operator(Integer a, Integer b, BiFunction<Integer, Integer, Integer> bf) {
        return bf.apply(a, b);
    }
}

原文地址:
https://www.yuque.com/books/share/94d7e9ce-40a5-4878-97f2-c6297f679886

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值