函数式接口
只有一个方法的接口
// 例如Runnable接口
//注解的含义:函数式接口
@FunctionalInterface
public interface Runnable {
public abstract void run();
}
四大函数式接口
1、Function:函数型接口
public interface Function<T, R> {
R apply(T t); // 传入参数T,返回参数R
2、Predicate:断定型接口
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t); //传入一个参数,返回一个布尔值
// 可用于判断字符串是否为空
3、Consumer:消费型接口
@FunctionalInterface
public interface Consumer<T> {
void accept(T t); //传入一个参数,没有返回值
4、Supplier:供给型接口
@FunctionalInterface
public interface Supplier<T> {
T get(); //get()方法没有参数,只有返回值
// 传入的泛型表示返回的类型
}