Java之函数式接口(FunctionalInterface)

步兴德
2023-12-01

Java之函数式接口(FunctionalInterface)

1. FunctionalInterface是什么?

FunctionalInterface 中文称之为 函数式接口。是Java新特性之一,所谓的函数式接口,只能有一个抽象方法的接口(Interface)

2. FunctionalInterface有什么作用?

作为Java的新特性之一,主要是标识某一类接口,如果满足以上特性(只能有一个抽象方法的接口(Interface)), 就可以使用@FunctionalInterface注解。

一般是配合Lambda表达式一起使用。

例如

@FunctionInterface
public interface Handler{
    void apply();
}
// Lambda表达式调用
() -> {System.out.println("输出")}

3. 常见的FunctionalInterface

Java提供了一系列常见的函数式编程供我们使用。下面举例。

1. Supplier

提供者,如其名,不接收参数,返回T类型对象

@FunctionalInterface
public interface Supplier<T> {

    /**
     * Gets a result.
     *
     * @return a result
     */
    T get();
}

2. Consumer

消费者,如其名,接收一个参数对象T, 不返回结果,主要针对处理部分业务场景

@FunctionalInterface
public interface Consumer<T> {

    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);

    /**
     * Returns a composed {@code Consumer} that performs, in sequence, this
     * operation followed by the {@code after} operation. If performing either
     * operation throws an exception, it is relayed to the caller of the
     * composed operation.  If performing this operation throws an exception,
     * the {@code after} operation will not be performed.
     *
     * @param after the operation to perform after this operation
     * @return a composed {@code Consumer} that performs in sequence this
     * operation followed by the {@code after} operation
     * @throws NullPointerException if {@code after} is null
     */
    // andThen 处理两步 接收一个Consumer对象 先处理accept 在处理 consumer里面的accept 相当于处理两步
    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}

3. Function

方法,如其名,接收一个参数T 返回一个结果R

@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 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)
     */
	// 如其名 andThen 在执行apply之后 对返回结果 R V,返回V 
    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;
    }

4. Predicate

预测,如其名,接收一个参数T 返回boolean值

@FunctionalInterface
public interface Predicate<T> {

    /**
     * Evaluates this predicate on the given argument.
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(T t);

}

5. 其他

其他就是指上面几大接口的衍生接口,例如Supplier衍生接口,IntSupplier 返回int类型,LongSupplier返回long类型。DoubleSupplier返回Double类型

BooleanSupplier返回boolean类型。其他Function、Consumer、Predicate都类似,主要是处理基础类型

注意Function衍生,例如LongFunction 接收值是long 返回值是R

4. 如何自定义FunctionalInterface

上面说到什么是FunctionalInterface接口,那么我们可以根据自己的业务以及定义规则去设置一个FunctionalInterface

@FunctionalInterface
public interface Filter<T> {
    
    void filter(T t);
}

官方以及effective建议 尽量使用默认的FunctionalInterface接口 如果实在不满足自身业务需求,再创建新的FunctionalInterface

5. 总结

FunctionalInterface函数式接口,其实是一种标识定义,一般是结合Lambdabiao表达式,以前使用匿名函数去实现,现在直接可以使用Lambda表达式,非常简单使用。

从理解上来看,其实个人觉得还没有匿名函数或者实现接口的方式更好理解。只是这样代码可读性好看了不少。一般被FunctionalInterface注解的 基本是结合Lambda 不去实现

在Java中目前这种模式 被大量广泛的使用。例如Comparator

 类似资料: