函数接口

简单的一个实例:

public class JavaTest {

     public static void main(String[] args) {

        JavaTest javaTest = new JavaTest();
        System.out.println("计算结果:"+ javaTest.computer(10,i -> i*i));

        System.out.println("计算结果:" + javaTest.computer(10,a -> a+a));

    }

    public int computer(int a,Function<Integer,Integer> function){
        int result = function.apply(a);
        return result;
    }
}

    /**
     * Applies this function to the given argument.
     *
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);
public interface Function<T, R>

Function中apply方法接收一个T,返回一个R,对应着Function<T, R>,T代表入参,R代表返回类型。


Function中另外两个方法:
    public static void main(String[] args) {

        JavaTest javaTest = new JavaTest();
        System.out.println("计算结果:"+ javaTest.computer1(10,a -> a * a ,a -> a+a));

        System.out.println("计算结果:" + javaTest.computer2(10,a -> a * a ,a -> a+a));

    }

    public int computer1(int a ,Function<Integer,Integer> function1,Function<Integer,Integer>function2){
        return function1.compose(function2).apply(a);
    }

    public int computer2(int a ,Function<Integer,Integer> function1,Function<Integer,Integer>function2){
        return function1.andThen(function2).apply(a);
    }
    /**
     * 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));
    }
compose先执行before方法,再将其返回值作为入参执行原function方法。

    /**
     * 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));
    }
andThen方法,先执行原function方法,再将其返回值作为入参执行after方法

System.out.println("计算结果:"+ javaTest.computer1(10,a -> a * a ,a -> a+a));
10 + 10 -> 20 * 20 = 400

System.out.println("计算结果:" + javaTest.computer2(10,a -> a * a ,a -> a+a));
10 * 10 -> 100 + 100 = 200




原文地址:https://www.cnblogs.com/zhvip/p/12830820.html