java8函数编程--BiFunction

BiFunction和Function函数一样都是有入参和返回值的函数,不同的是BiFunction有两个入参

@FunctionalInterface
public interface BiFunction {

    /**
     * Applies this function to the given arguments.
     *
     * @param t the first function argument
     * @param u the second function argument
     * @return the function result
     */
    R apply(T t, U u);

    /**
     * Returns a composed function that first applies this function to
     * its input, and then applies the {@code after} function to the result.
     * If evaluation of either function throws an exception, it is relayed to
     * the caller of the composed function.
     *
     * @param  the type of output of the {@code after} function, and of the
     *           composed function
     * @param after the function to apply after this function is applied
     * @return a composed function that first applies this function and then
     * applies the {@code after} function
     * @throws NullPointerException if after is null
     */
    default  BiFunction andThen(Function after) {
        Objects.requireNonNull(after);
        return (T t, U u) -> after.apply(apply(t, u));
    }
}

示例:

public class Student {
    String name;
    Integer age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public static Student build(String name,Integer age,BiFunction biFunction){
        return biFunction.apply(name,age);
    }
}
public class ClassRoom {

    List studentList ;
    public ClassRoom() {
         studentList = new ArrayList<>();
         Student s1 = new Student();
         s1.setAge(10);
         s1.setName("s1");

         Student s2 = new Student();
         s2.setAge(11);
         s2.setName("s2");
         studentList.add(s1);
         studentList.add(s2);
    }

    /**
     * 按年龄筛选学生
     * @param age
     * @param students
     * @param biFunction
     * @return
     */
    public List findStudentByAge(Integer age, List students, BiFunction,List> biFunction){
        return biFunction.apply(age,students);
    }
    public List getStudentList() {
        return studentList;
    }
    public void setStudentList(List studentList) {
        this.studentList = studentList;
    }
    public  Integer getStudentAgeByName(){
        if(CollectionUtils.isEmpty(studentList)){
            return null;
        }
        studentList.sort(Comparator.comparing(Student::getAge).reversed());
        return studentList.get(0).getAge();
    }
}

    @Test
    public void biFunctionTest(){
        ClassRoom classRoom = new ClassRoom();
        List resultList = classRoom.findStudentByAge(10,classRoom.getStudentList(),(age, list)->
            list.stream().filter(student -> student.getAge()<=age).collect(Collectors.toList())
        );
        System.out.println(JSON.toJSONString(resultList));  //[{"age":10,"name":"s1"}]
    }

你可能感兴趣的:(java8函数编程--BiFunction)