Java8 BinaryOperator

package com.lgx.jdk8.part01;

import java.util.Comparator;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;

/**
 * BinaryOperator的讲解
 */
public class Test13BinaryOperator {
    public static void main(String[] args) {
        Test13BinaryOperator test = new Test13BinaryOperator();
        Double result = test.biFunctionCompute(10.0, 15.0, (value1, value2) -> value1 + value2);
        System.out.println("result = [" + result + "]");

        Double result2 = test.binaryOperatorCompute(10.0, 15.0, (value1, value2) -> value1 + value2);
        System.out.println("result2 = [" + result2 + "]");

        Double minResult = test.binaryOperatorMinBy(100.0, 150.0, Double::compareTo);
        System.out.println("数字较小minResult = [" + minResult + "]");

        Double maxResult = test.binaryOperatorMaxBy(100.0, 150.0, Double::compareTo);
        System.out.println("数字较大maxResult = [" + maxResult + "]");

        String minResult2 = test.binaryOperatorMinBy2("hello", "hello world", (a, b) -> a.length() - b.length());
        System.out.println("长度较短minResult2 = [" + minResult2 + "]");
        String minResult3 = test.binaryOperatorMinBy2("hello", "hello world", String::compareTo);
        System.out.println("长度较短minResult3 = [" + minResult3 + "]");
        String minResult4 = test.binaryOperatorMinBy2("hello", "world123", (a, b) -> a.charAt(0) - b.charAt(0));
        System.out.println("首字母在前面的minResult4 = [" + minResult4 + "]");

        String maxResult2 = test.binaryOperatorMaxBy2("hello", "hello world", String::compareTo);
        System.out.println("长度较长maxResult2 = [" + maxResult2 + "]");
    }

    //BiFunction写法
    public Double biFunctionCompute(Double para1, Double para2, BiFunction<Double, Double, Double> biFunction){
        return biFunction.apply(para1, para2);
    }

    //BinaryOperator写法
    public Double binaryOperatorCompute(Double para1, Double para2, BinaryOperator<Double> binaryOperator){
        return binaryOperator.apply(para1, para2);
    }

    //获取2个double中较小的那个
    public Double binaryOperatorMinBy(Double para1, Double para2, Comparator<Double> comparator){
        return BinaryOperator.minBy(comparator).apply(para1, para2);
    }

    //获取2个double中较大的那个
    public Double binaryOperatorMaxBy(Double para1, Double para2, Comparator<Double> comparator){
        return BinaryOperator.maxBy(comparator).apply(para1, para2);
    }

    public String binaryOperatorMinBy2(String para1, String para2, Comparator<String> comparator){
        return BinaryOperator.minBy(comparator).apply(para1, para2);
    }

    public String binaryOperatorMaxBy2(String para1, String para2, Comparator<String> comparator){
        return BinaryOperator.maxBy(comparator).apply(para1, para2);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
BinaryOperatorJava 8中的一个函数式接口,它表示一个接受两个参数并返回一个结果的函数。BinaryOperator接口扩展了Function接口,它有一个apply方法,用于接受两个参数并返回一个结果。 BinaryOperator的常用方法包括: 1. apply方法:接受两个参数并返回一个结果。 2. maxBy方法:返回一个BinaryOperator对象,该对象返回两个参数中较大的那个。 3. minBy方法:返回一个BinaryOperator对象,该对象返回两个参数中较小的那个。 4. identity方法:返回一个BinaryOperator对象,该对象返回其第一个参数。 下面是每个方法的详细说明: 1. apply方法: BinaryOperator接口继承了Function接口,因此它继承了Function接口的apply方法。apply方法接受两个参数并返回一个结果。例如: BinaryOperator<Integer> add = (x, y) -> x + y; int result = add.apply(3, 5); // result = 8 2. maxBy方法: maxBy方法返回一个BinaryOperator对象,该对象返回两个参数中较大的那个。例如: BinaryOperator<Integer> max = BinaryOperator.maxBy((x, y) -> x - y); int result = max.apply(3, 5); // result = 5 3. minBy方法: minBy方法返回一个BinaryOperator对象,该对象返回两个参数中较小的那个。例如: BinaryOperator<Integer> min = BinaryOperator.minBy((x, y) -> x - y); int result = min.apply(3, 5); // result = 3 4. identity方法: identity方法返回一个BinaryOperator对象,该对象返回其第一个参数。例如: BinaryOperator<Integer> identity = BinaryOperator.identity(); int result = identity.apply(3, 5); // result = 3 这些是BinaryOperator的常用方法,它们可以帮助我们轻松地执行二元操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值