函数式接口
- 有且仅有一个方法的接口
- 可以使用 Lambda 简化
Function
- 需要提供一个输入和一个输出
- 可以理解为就是一个函数
public interface Function<T, R> {
R apply(T t);
}
public static void main(String[] args) {
Function<String, String> function = o -> o;
System.out.println(function.apply("hello world"));
}
Function<String, String> function = (o) -> {
return o;
};
Predicate
public interface Predicate<T> {
boolean test(T t);
}
Predicate<String > predicate= "hello"::equals;
System.out.println(predicate.test("test"));
Consumer
public interface Consumer<T> {
void accept(T t);
}
Consumer<String> consumer = System.out::println;
consumer.accept("hello");
Supplier
public interface Supplier<T> {
T get();
}
Supplier<String> stringSupplier = () -> "hello";
System.out.println(stringSupplier.get());