BinaryOperator功能接口Java示例

时间:2020-01-09 10:35:15  来源:igfitidea点击:

在这篇文章中,我们将看到Java java.util.function.BinaryOperator功能接口的示例。

BinaryOperator功能接口表示对两个相同类型的操作数的操作,返回与该操作数相同类型的结果。 BinaryOperator扩展了java.util.function.BiFunction接口,并为操作数和结果均为同一类型的情况提供行为。由于它扩展了BiFunction,因此继承了BiFunction接口的所有方法

  • T apply(T t1,T t2)–这里T表示参数的类型和返回类型。这是此功能界面中的抽象方法。如果我们要编写一个Lambda表达式,该表达式需要两个相同类型的参数并返回与传递的参数相同类型的值,则可以将该Lambda表达式编写为BinaryOperator内置功能接口的实现。
  • andThen(Function <?super R ,?在之后扩展V>)–以另一个Function为参数,并返回一个组合的BiFunction,该BiFunction依次执行首先调用BiFunction的操作,然后执行after操作。

除了以上两个继承的方法之外,BinaryOperator还具有以下静态方法。

  • minBy(Comparator <?super T>比较器)–返回BinaryOperator,该BinaryOperator根据指定的Comparator返回两个元素中的较小者。
  • maxBy(Comparator <?super T>比较器)–返回BinaryOperator,该BinaryOperator根据指定的Comparator返回两个元素中的较大者。

BinaryOperator接口apply()方法示例

这是一个简单的示例,其中apply方法被实现为lambda表达式,该表达式返回传递的整数之和。

import java.util.function.BinaryOperator;

public class BinaryOperatorExample {
  public static void main(String[] args) {
    BinaryOperator<Integer> binaryOperator = (a,b) -> a+b;
    System.out.println("Result- " + binaryOperator.apply(2,3));
    System.out.println("Result- " + binaryOperator.apply(9, 10));
  }
}

输出:

Result- 5
Result- 19

BinaryOperator接口和Then()方法示例

在示例中,添加了传递的参数,然后对结果求平方,并使用andThen()方法将其作为一系列操作来完成。

import java.util.function.BinaryOperator;
import java.util.function.Function;

public class BinaryOperatorExample {
  public static void main(String[] args) {
    BinaryOperator<Integer> binaryOperator1 = (a,b) -> a+b;
    Function<Integer, Integer> function = (n) -> n*n;
    System.out.println("Result- " + binaryOperator1.andThen(function).apply(2,3));
    System.out.println("Result- " + binaryOperator1.andThen(function).apply(9, 10));
  }
}

输出:

Result- 25
Result- 361

BinaryOperator接口minBy()和maxBy()方法示例

使用BinaryOperator的minBy()和maxBy()方法,我们将从员工列表中获取最低薪水和最高薪水的员工。这两种方法都需要Comparator实现。请注意,这两种方法在BinaryOperator中均作为静态接口方法实现,这意味着接口中这些方法已经实现。

员工阶层

public class Employee {
  private String name;
  private String dept;
  private Integer salary;

  Employee(String name, String dept, Integer salary){
    this.name = name;
    this.dept = dept;
    this.salary = salary;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public Integer getSalary() {
    return salary;
  }
  public void setSalary(Integer salary) {
    this.salary = salary;
  }
  public String getDept() {
    return dept;
  }
  public void setDept(String dept) {
    this.dept = dept;
  }
}
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.BinaryOperator;
import java.util.function.Function;

public class BinaryOperatorExample {
  public static void main(String[] args) {
    List<Employee> employeeList = new ArrayList<>(); 
    // Preparing List
    employeeList.add(new Employee("Hyman", "Finance", 5500)); 
    employeeList.add(new Employee("Lisa", "Finance", 5600)); 
    employeeList.add(new Employee("Scott", "Finance", 7000));
    employeeList.add(new Employee("Nikita", "IT", 4500));
    employeeList.add(new Employee("Tony", "IT", 8000));
    // Comparator implementation
    Comparator<Employee> comparator = (Employee e1, Employee e2) -> e1.getSalary().compareTo(e2.getSalary());
    BinaryOperator<Employee> binOperatorMin = BinaryOperator.minBy(comparator);
    BinaryOperator<Employee> binOperatorMax = BinaryOperator.maxBy(comparator);
    Employee emp = findByComparison(employeeList, binOperatorMin);
    System.out.println("Employee with min salary: Name- " + emp.getName() + " Salary-" + emp.getSalary());
    emp = findByComparison(employeeList, binOperatorMax);
    System.out.println("Employee with max salary: Name- " + emp.getName() + " Salary-" + emp.getSalary());
  }

  public static Employee findByComparison(List<Employee> employeeList, BinaryOperator<Employee> binOperator){
    Employee emp = null;
    for(Employee e: employeeList) {
      if(emp == null) {
        emp = e;
      }
      else {
        emp = binOperator.apply(emp, e);
      }
    }
    return emp;
  }
}

输出:

Employee with min salary: Name- Nikita Salary-4500
Employee with max salary: Name- Tony Salary-8000