美文网首页
java8函数编程--Function

java8函数编程--Function

作者: clicsug | 来源:发表于2019-11-05 17:57 被阅读0次

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

相关文章

  • java8函数式编程内置函数小结

    java8函数式编程 Function

  • Java8 函数式编程(一)——Function<? su

    Java8 函数式编程(一)——Function mapper 关...

  • Lambda重构设计模式

    引言: 最近回看了一遍等书,加上平时自己function编程写的很多,写...

  • java8函数编程--Function

    Function Function是java8的一个函数式编程接口 apply方法接收一个入参T,返回R,在使用上...

  • Java8的接口

    但是java8里面的函数式编程包function下面的接口,里面都有默认实现。 我还是觉得,还是分开比较好。接口和...

  • JAVA 8 forkjoin实际体验

    JAVA8 函数编程 -都说java8 新的函数式编程特别是并行流式编程,但是并行流的性能并不一定就好 没有很复杂...

  • java8中的Stream

    java8提出的函数式编程旨在帮助程序猿们写出更优雅的代码,上文函数式编程基础也介绍了java8新提出的一些函数式...

  • Python-函数式编程

    函数式编程简介 一、什么是函数式编程? 函数:function函数式:functional,是一种编程范式 二、函...

  • Java函数式编程(待续)

    函数式编程概念 ​ 面向对象抽象数据,函数式编程抽象行为。---摘自《on java8》 ​ 下面是廖雪峰...

  • java8函数式编程-Function

    闲话少说 上来就是干啊下图是 Function 的 说明image.png 接收一个参数 返回一个值

网友评论

      本文标题:java8函数编程--Function

      本文链接:https://www.haomeiwen.com/subject/kksebctx.html