BinaryOperator函数式接口

1.BinaryOperator的定义

@FunctionalInterface
public interface BinaryOperator<T> extends BiFunction<T, T, T> {
    static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) {
        Objects.requireNonNull(comparator);
        return (a, b) -> {
            return comparator.compare(a, b) <= 0 ? a : b;
        };
    }

    static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) {
        Objects.requireNonNull(comparator);
        return (a, b) -> {
            return comparator.compare(a, b) >= 0 ? a : b;
        };
    }
}
  • 从上面不难发现,BinaryOperator继承了BiFunction,并且多了二个静态方法minBy和maxBy。
  • minBy和maxBy有很意思,返回了一个BiFunction对应的Lambda,Lambda内部使用传入的comparator比较器比较,然后在进一步处理返回最大值还是最小值。

2.更多例子

public class BinaryOperatorTest {
    public static void main(String[] args){
        BinaryOperatorTest test = new BinaryOperatorTest();
        test.testBinaryOperator(1,2,(num1,num2)->num1 + num2); //3
        test.testBinaryOperator(4,2,(num1,num2)->num1 - num2); //2
        test.testBinaryOperator(3,2,(num1,num2)->num1 * num2); //6
        test.testBinaryOperator(8,2,(num1,num2)->num1 / num2); //4
        test.testMinBy("hello","wonders",(str1,str2)->str1.length()-str2.length()); //hello
        //方法引用
        test.testMinBy("hello","wonders", Comparator.comparingInt(String::length)); //hello
        test.testMinBy("hello","wonders",(str1,str2)->str1.charAt(0)-str2.charAt(0)); //hello
        //方法引用
        test.testMinBy("hello","wonders", Comparator.comparingInt(str -> str.charAt(0))); //hello
        test.testMaxBy("hello","wonders",(str1,str2)->str1.length()-str2.length()); //wonders
        //方法引用
        test.testMaxBy("hello","wonders", Comparator.comparingInt(String::length)); //wonders
        test.testMaxBy("hello","wonders",(str1,str2)->str1.charAt(0)-str2.charAt(0)); //wonders
        //方法引用
        test.testMaxBy("hello","wonders", Comparator.comparingInt(str -> str.charAt(0))); //wonders
    }

    public void testBinaryOperator(Integer num1,Integer num2,BinaryOperator<Integer> result){
        System.out.println(result.apply(num1,num2));
    }

    /**
     * 返回两者里面较小的一个
     * @param str1
     * @param str2
     * @param comparator
     */
    public void testMinBy(String str1, String str2, Comparator<String> comparator){
        System.out.println(BinaryOperator.minBy(comparator).apply(str1,str2));
    }

    /**
     * 返回两者里面较大的一个
     * @param str1
     * @param str2
     * @param comparator
     */
    public void testMaxBy(String str1, String str2, Comparator<String> comparator){
        System.out.println(BinaryOperator.maxBy(comparator).apply(str1,str2));
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值