java8函数编程--Function

Function

Function是java8的一个函数式编程接口

@FunctionalInterface
public interface Function<T, R> {

    /**
     * Applies this function to the given argument.
     *
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);

    /**
     * Returns a composed function that first applies the {@code before}
     * function to its input, and then applies this function to the result.
     * If evaluation of either function throws an exception, it is relayed to
     * the caller of the composed function.
     *
     * @param <V> the type of input to the {@code before} function, and to the
     *           composed function
     * @param before the function to apply before this function is applied
     * @return a composed function that first applies the {@code before}
     * function and then applies this function
     * @throws NullPointerException if before is null
     *
     * @see #andThen(Function)
     */
    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

    /**
     * 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 <V> 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
     *
     * @see #compose(Function)
     */
    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }

    /**
     * Returns a function that always returns its input argument.
     *
     * @param <T> the type of the input and output objects to the function
     * @return a function that always returns its input argument
     */
    static <T> Function<T, T> identity() {
        return t -> t;
    }
}

apply方法接收一个入参T,返回R,在使用上和理解上类似于我们上学时学的函数 y=f(x),可以将我们定义的函数方法作用在入参T上,函数的返回值就是R

public class FunctionPractice {
    
    public static <T> Integer getMaxAgeFromClassRoom(T t,Function<T,Integer> func){
        return func.apply(t);
    }
}

public class ClassRoom {

    List<Student> 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);
     }


    public  Integer getStudentAgeByName(){
        if(CollectionUtils.isEmpty(studentList)){
            return null;
        }
        studentList.sort(Comparator.comparing(Student::getAge).reversed());

        return studentList.get(0).getAge();
    }
}
public class FunctionTest {

    @Test
    public void test(){
        ClassRoom classRoom = new ClassRoom();
        Integer age = FunctionPractice.getMaxAgeFromClassRoom(classRoom,ClassRoom::getStudentAgeByName);
        System.out.println("max age = "+age);
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 13,952评论 0 38
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 2,060评论 0 2
  • 最近看了慕课网的《SpringBoot2.0不容错过的新特性 WebFlux响应式编程》教程,里面介绍了关于Jav...
    逆水寻洲阅读 1,114评论 0 1
  • 函数只定义一次,但可能被执行或调用任意次。JS函数是参数化的,函数的定义会包括一个称为形参的标识符列表,这些参数在...
    PySong阅读 873评论 0 0
  • 函数只定义一次,但可能被执行或调用任意次。JS函数是参数化的,函数的定义会包括一个称为形参的标识符列表,这些参数在...
    PySong阅读 558评论 0 0