函数式接口

一、函数式接口基础

  • A functional interface has exactly one abstract method. Since default methods have an implementation, they are not abstract. If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.

函数式接口只允许一个抽象的方法,可以有默认方法,也可以实现Object中的方法。

@FunctionalInterface
public interface MyFunctionalInterface {

    void myMethod();

    default void myDefaultMethod() {
        System.out.println("default moethod");
    }

    @Override
    String toString();

}
  • The compiler will treat any interface meeting the definition of a functional interface as a functional interface regardless of whether or not a FunctionalInterface annotation is present on the interface declaration.

不管有没有加@FunctionalInterface,编译器都会把只有一个抽象方法的接口作为函数式接口。

  • Note that instances of functional interfaces can be created with lambda expressions, method references, or constructor references.

函数式接口的实例可以用lambda表达式、方法引用、构造方法引用的方式被创建。

new Thread(() -> System.out.println("I'm a new thread.")).start();

lambda表达式

Kind Example
Reference to a static method ContainingClass::staticMethodName
Reference to an instance method of a particular object containingObject::instanceMethodName
Reference to an instance method of an arbitrary object of a particular type ContainingType::methodName
Reference to a constructor ClassName::new

Kinds of Method References

  • Java8中实现函数式编程的,将函数作为参数传递给一个方法、声明返回一个函数的方法,都是利用函数式接口实现的,直接把函数式接口作为方法的形参和返回值去实现。
default void forEach(Consumer<? super T> action) {
    Objects.requireNonNull(action);
    for (T t : this) {
        action.accept(t);
    }
}

IterableforEach方法中,参数Consumer就是一个函数式接口,这就是传递的是一种行为。

List<String> list = Arrays.asList("a", "b", "c");
list.forEach(s -> System.out.println(s.toUpperCase()));

这个调用就是传递进去一种对集合中的字符串大写输出的一种行为。

  • 在别的函数式编程语言中,Lambda表达式的类型是函数,但是在Java中Lambda表达式的类型是对象,它所属的类型就是函数式接口。
Consumer<String> consumer = s -> System.out.println(s.toUpperCase());

二、常用的函数式接口

  • Function<T, R>
R apply(T t);

Function 函数式接口的抽象方法为 apply 接受一个参数,返回一个结果。

Function<String, Integer> function = v -> v.length();

此外该函数式接口还有两个默认方法

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

compose 默认方法先调用参数的 apply 方法,然后再调用本身的 apply 方法,注意:此方法返回的是 Function 函数式接口

andThen 默认方法先调用本身的 apply 方法,然后再调用参数的 apply 方法,注意:此方法返回的也是 Function 函数式接口

  • BiFunction<T, U, R>
R apply(T t, U u);

BiFunction 函数式接口的抽象方法为 apply 接受两个参数,返回一个结果。

BiFunction<Integer, Integer, Integer> biFunction = (a, b) -> a + b;

该函数式接口有一个默认方法

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

andThen 默认方法先调用本身的 apply 方法,这时会返回一个结果,然后再调用类型为 Function 的函数式接口的 apply 方法,返回一个结果,这样等于传入两个参数最后返回一个结果,符合BiFunctionapply 的定义。注意:此方法返回的是 BiFunction 函数式接口

  • Predicate<T>
boolean test(T t);

Predicate 函数式接口的抽象方法为 test 接受一个参数,返回一个布尔值。

Predicate<Integer> predicate = p -> p > 10;

该函数式接口有三个默认方法,这三个默认方法见名知意

default Predicate<T> and(Predicate<? super T> other) {
    Objects.requireNonNull(other);
    return (t) -> test(t) && other.test(t);
}

default Predicate<T> negate() {
    return (t) -> !test(t);
}

default Predicate<T> or(Predicate<? super T> other) {
    Objects.requireNonNull(other);
    return (t) -> test(t) || other.test(t);
}

static <T> Predicate<T> isEqual(Object targetRef) {
    return (null == targetRef)
            ? Objects::isNull
            : object -> targetRef.equals(object);
}

此外还有一个静态方法 isEqual这个方法返回的也是一个函数式接口,返回的是一种行为,object -> targetRef.equals(object) 返回这个Lambda表达式就是一个 Predicate 的具体实现。所以这里边这个 object 参数就是 Predicate 函数式接口 test 的参数。

  • Supplier<T>
T get();

Supplier 函数式接口的抽象方法为 get 不接受参数,返回一个对象。

Supplier<String> supplier = () -> "Supplier";

这个函数式相对来说简单一点,只包含了一个方法。

  • BinaryOperator<T> extends BiFunction<T,T,T>

这个函数式接口是 BiFunction 的一种特例,两个参数和一个返回都为一个类型。

BinaryOperator<Integer> binaryOperator = (a, b) -> a + b;
  • Consumer<T>
void accept(T t);

Consumer 函数式接口的抽象方法为 accept 接受一个参数,没有返回。

Consumer<String > consumer = c -> System.out.println(c);

该函数式接口有一个默认方法

default Consumer<T> andThen(Consumer<? super T> after) {
    Objects.requireNonNull(after);
    return (T t) -> { 
        accept(t); 
        after.accept(t); 
    };
}

该默认方法先调用本身的 accept 方法,然后再调用参数的 accept 方法,传入一个参数不返回结果符合 Consumeraccept 的定义。注意:此方法返回的是 Consumer 函数式接口

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 简介 概念 Lambda 表达式可以理解为简洁地表示可传递的匿名函数的一种方式:它没有名称,但它有参数列表、函数主...
    刘涤生阅读 3,261评论 5 18
  • lambda表达式(又被成为“闭包”或“匿名方法”)方法引用和构造方法引用扩展的目标类型和类型推导接口中的默认方法...
    183207efd207阅读 1,504评论 0 5
  • 原文链接: Lambdas 原文作者: shekhargulati 译者: leege100 lambda表达式是...
    忽来阅读 6,662评论 8 129
  • 云槿问出这几句话,把身子挺直起来,严肃地看着荆舟。她万万想不到荆舟会打她的主意,想让她帮他做事业。她也很难理解,一...
    冬妮娅阅读 166评论 0 0
  • 早上起来读了一篇文,很想分享给现在教的小朋友们,但一走进课堂,他们无精打采的听着课,顿时就没多大意愿分享了。上厕所...
    佘小秋阅读 350评论 12 23