jdk8 新特性 Function接口

375 阅读2分钟

「这是我参与11月更文挑战的第3天,活动详情查看:2021最后一次更文挑战

Function接口

jdk1.8 新增加了一个函数式接口Function

<T,R>代表两个泛型参数T,R,传入的类型不限.

image.png

接口结构解析

  • 1.接口中没有抽象方法?

接口的方法就是隐式抽象,因此 apply就是抽象方法.其他的方法都是在实现这个方法.

  • 2.接口中的其他方法有方法体?

其中compose和andThen使用default修饰,实现方法体.而identity为什么也有方法体呢?

jdk1.8中,接口中可以有静态方法,必须有body

@FunctionalInterface
public interface Function<T, R> {

    R apply(T t);)

    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }

    static <T> Function<T, T> identity() {
        return t -> t;
    }
}

apply

参数为T,返回结果为R,有输入有输出.

R apply(T t);

这其实就相当于一个函数:𝑦=𝑓(𝑥).具体函数想要实现什么就要自己编写.

// 相当于函数 y = x + 1;		
Function<Integer,Integer> function = i -> i + 1;

compose

(参数) → {过程}

左边的(V v)就是传入的参数,先将v参数带入before的apply方法中,然后function2 再对结果执行apply方法,最后再返回执行的结果

将function作为参数传入function2的compose方法:先执行function的apply的方法,然后再将执行结果作为参数执行functionapply方法

// compose第一个参数是Function类型,而Function的第一个参数必须是V的父类,第二个参数是T的子类
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

1+11 = 12 → 12 * 2 = 24

				Function<Integer,Integer> function = i -> i + 11;
        Function<Integer,Integer> function2 = i -> i * 2;
        //function作为参数传入function2的compose方法:先执行function的apply的方法,然后再将执行结果作为参数执行functionapply方法
        Integer apply = function2.compose(function).apply(1);
        System.out.println(apply);

andThen

本函数先执行apply参数,然后将结果带入after的apply参数中

default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }

1 * 2 = 2 → 2 + 11 = 13

				Integer apply1 = function2.andThen(function).apply(1);
        System.out.println(apply1);