Java8中BinaryOperator的功能及示例说明

乔欣 Java经验 发布时间:2023-01-31 17:18:45 阅读数:17687 1
下文笔者讲述BinaryOperator的功能及示例说明,如下所示

BinaryOperator接口功能简介

BinaryOperator是一个功能接口
   它扩展BiFunction 
BinaryOperator:
   接受两个相同类型的参数
   并返回其参数相同类型的结果

BinaryOperator源码

@FunctionalInterface
public interface BinaryOperator<T> extends BiFunction<T,T,T> {
}

@FunctionalInterface
public interface BiFunction<T, U, R> {
      R apply(T t, U u);
}

BiFunction接受两个任意类型的参数
  并返回任意类型的结果

BinaryOperator示例

package com.java265;
 
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
public class Java8BinaryOperator1 {
    public static void main(String[] args) {
        // BiFunction
        BiFunction<Integer, Integer, Integer> func = (x1, x2) -> x1 + x2;
        Integer result = func.apply(2, 3);
        System.out.println(result); // 5
 
        // BinaryOperator
        BinaryOperator<Integer> func2 = (x1, x2) -> x1 + x2;
        Integer result2 = func.apply(2, 3);
        System.out.println(result2); // 5
    } 
}

IntBinaryOperator

package com.java265;
 
import java.util.function.IntBinaryOperator;
import java.util.stream.IntStream;
 
public class Java8BinaryOperator3 {
 
    public static void main(String[] args) {
 
        int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 
        int result = math((numbers), 0, (a, b) -> a + b);
 
        System.out.println(result); // 55
 
        int result2 = math((numbers), 0, Integer::sum);
 
        System.out.println(result2); // 55
 
        IntStream
    }
 
    public static int math(int[] list, int init, IntBinaryOperator accumulator) {
        int result = init;
        for (int t : list) {
            result = accumulator.applyAsInt(result, t);
        }
        return result;
    } 
}

BinaryOperator.maxBy()和BinaryOperator.minBy()

package com.java265;
 
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.BinaryOperator;
 
public class Java8BinaryOperator4 {
 
    public static void main(String[] args) {
 
        Developer dev1 = new Developer("jordan", BigDecimal.valueOf(1111));
        Developer dev2 = new Developer("jack", BigDecimal.valueOf(2222));
        Developer dev3 = new Developer("jaden", BigDecimal.valueOf(38000));
        Developer dev4 = new Developer("ali", BigDecimal.valueOf(16000));
        Developer dev5 = new Developer("java265", BigDecimal.valueOf(1));
 
        List<Developer> list = Arrays.asList(dev1, dev2, dev3, dev4, dev5);
 
        // 1. Create a Comparator
        Comparator<Developer> comparing = Comparator.comparing(Developer::getSalary);
 
        // 2. BinaryOperator with a custom Comparator
        BinaryOperator<Developer> bo = BinaryOperator.maxBy(comparing);
 
        Developer result = find(list, bo);
 
        System.out.println(result);     // Developer{name='jaden', salary=10000}
 
        // one line
 
        // find developer with highest pay
        Developer developer = find(list, BinaryOperator.maxBy(Comparator.comparing(Developer::getSalary)));
        System.out.println(developer);  // Developer{name='jaden', salary=10000}
 
        // find developer with lowest pay
        Developer developer2 = find(list, BinaryOperator.minBy(Comparator.comparing(Developer::getSalary)));
        System.out.println(developer2); // Developer{name='java265', salary=1}
 
    }
 
    public static Developer find(List<Developer> list, BinaryOperator<Developer> accumulator) {
        Developer result = null;
        for (Developer t : list) {
            if (result == null) {
                result = t;
            } else {
                result = accumulator.apply(result, t);
            }
        }
        return result;
    }
 
}


package com.java265; 
import java.math.BigDecimal;
public class Developer {
 
    String name;
    BigDecimal salary;
 
    public Developer(String name, BigDecimal salary) {
        this.name = name;
        this.salary = salary;
    } 
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202301/16751567905598.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者