java8 함수 프로그래밍 --BiFunction

3380 단어
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 super R, ? extends V> 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"}]
    }

좋은 웹페이지 즐겨찾기