美文网首页
JDK8_函数式接口Function详解

JDK8_函数式接口Function详解

作者: 小安的大情调 | 来源:发表于2019-12-19 19:11 被阅读0次

    努力努力再努力xLg

    [TOC]

    函数式接口的定义

    • 函数式接口就是有且只有一个抽象方法的接口,都可以称之为函数式接口。
      • Runnable(),Comparator()都是函数式接口
    • 加了@functionalInterface注解。
    • 如果没有添加该注解,但是满足有且只有一个抽象方法的接口,都会被jdk8认为是函数式接口。
    • 在JDK8 之前,java方法之间的调用都只能传递参数,JDK8之后使用lambda函数式编程, 实现了方法之间传递行为。

    Function接口

    import java.util.Objects;
    
    java中所有的方法都是只能返回一个结果!!!
    
    /**
     * Represents a function that accepts one argument and produces a result.
     * 接受一个参数并且产生一个参数并且返回
     * <p>This is a <a href="package-summary.html">functional interface</a>
     * whose functional method is {@link #apply(Object)}.
     *
     * @param <T> the type of the input to the functionf
     * @param <R> the type of the result of the function
     *
     * @since 1.8
     */
    @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;
        }
    }
    
    

    在上述源码中,较为难理解的就是compose andThen 两个方法了。

    其实这两个方法都是为了使用方便而设计的,这是JDK8给我们提供的函数的复合,这意味着你可以把多个简单的Lambda复合成复杂的表达式。

    ​ 上述两个方法可以看作JDK为了将Lambda表达式复合起来,特意在Function接口中添加的两个默认方法。他们都会返回一个Function实例。

    举例说明

    ​ 假设一个函数f给数字做加1操作(x -> x + 1),另一个函数g给数字做乘法操作。使用上述两个方法就可以将这两个函数操作复合起来,是代码更加简洁。

    compose

    Function<Integer,Integer> f = x -> x + 1;
    Function<Integer,Integer> g = x -> x * 2;
    Function<Integer,Integer> h = f.compose(g);
    int result = h.apply(1);
    

    这里得到的答案是3

    这是可以看作一个数学的方程解答,使用compose可以看作为数学中的f(g(x))

    从源码的JavaDoc中也可以看出,首先调用该方法返回结果之前,将该结果作为参数,计算出最终结果

    andThen

    Function<Integer,Integer> f = x -> x + 1;
    Function<Integer,Integer> g = x -> x * 2;
    Function<Integer,Integer> h = f.andThen(g);
    int result = h.apply(1);
    

    这里得到的答案是4

    相当于数学中的g(f(x))

    compose刚好相反。

    andThen有然后的意思,即可以理解,先调用函数,得到一个适当的参数,然后用该方法本身,最后返回结果。compose反之。

    compose与andThen对比

    compose与andThen对比.png

    简单理解就是一个调用的先后顺序问题。

    研究BiFunction函数是接口

    /**
     * Represents a function that accepts two arguments and produces a result.
     * This is the two-arity specialization of {@link Function}.
     *
     * <p>This is a <a href="package-summary.html">functional interface</a>
     * whose functional method is {@link #apply(Object, Object)}.
     *
     * @param <T> the type of the first argument to the function
     * @param <U> the type of the second argument to the function
     * @param <R> the type of the result of the function
     *
     * @see Function
     * @since 1.8
     */
    @FunctionalInterface
    public interface BiFunction<T, U, R> {
    
        /**
         * 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 <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
         */
        default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {
            Objects.requireNonNull(after);
            return (T t, U u) -> after.apply(apply(t, u));
        }
    }
    

    可以看出该函数式接口的签名为(T,V)->R:接受两个类型参数,返回一个类型参数,相较于Function()接口多了一个输入参数。但是少了一个Compose方法;为什么呢?

    为什么BiFunction没有Compose方法

    假设如果有的话

        default <V> BiFunction<T,U, V> compose(Function<? super V, ? extends T> before) {
            Objects.requireNonNull(before);
            return (T t,U u) -> befor.apply(apply(t,u));
        }
    
    // Function 中的compose
        default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
            Objects.requireNonNull(before);
            return (V v) -> apply(before.apply(v));
        }
    

    尝试理解BiFunction中的andThen方法

    接受的参数是Function,因为java在返回结果中只能返回一个结果,使用andThen在计算完BiFunction中两个元素的运算之后只能返回一个结果并且赋值,如果按照这种逻辑,有compose方法的话,首先计算的是Function函数,并且只能返回一个结果,但是BiFunction中有两个参数需要赋值,所以不能满足该要求,所以不可能存在compose方法

            Function<Integer, Integer> f = x -> x + 1;
            Function<Integer, Integer> g = x -> x * 2;
            Function<Integer, Integer> h = f.andThen(g);
            int result = h.apply(1);
            System.out.println(result);
    
            BiFunction<Integer, Integer, Integer> ff = (x1, x2) -> x1 + x2 + 1;
            BiFunction<Integer, Integer, Integer> hh = ff.andThen(g);
            Integer apply = hh.apply(1, 2);
            System.out.println(apply);
    

    这里比较难以理解,画个重点。


    参考书籍

    ​ 《java8实战》

    本文仅供本人学习,一起进步!

    相关文章

      网友评论

          本文标题:JDK8_函数式接口Function详解

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