一、函数式接口基础
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);
}
}
在Iterable
的forEach
方法中,参数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
方法,返回一个结果,这样等于传入两个参数最后返回一个结果,符合BiFunction
的 apply
的定义。注意:此方法返回的是 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
方法,传入一个参数不返回结果符合 Consumer
的 accept
的定义。注意:此方法返回的是 Consumer
函数式接口