Java常用的函数式接口
// 过滤器接口,判断器接口,条件接口,有参数有返回值
@FunctionalInterface
public interface Predicate<T> {
/**
过滤器接口约束的方法,方法参数是用户使用时约束泛型对应具体数据参数
返回值类型是 boolean 类型
@param t 用户约束泛型对应的具体数据类型参数
@return boolean 数据,判断结果反馈
*/
boolean test(T t);
}
// 比较器接口,有参数有返回值
@FunctionalInterface
public interface Comparator<T> {
/**
比较器接口要求的方法,参数是泛型参数,用户指定类型
@param o1 用户在使用接口时约束的泛型对应具体数据类型参数
@param o2 用户在使用接口时约束的泛型对应具体数据类型参数
@return 返回值为 int 类型,0 表示两个元素一致
*/
int compare(T o1, T o2);
}
// 类型转换器接口,有参数有返回值
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
}
// 消费者接口,方法有参数无返回值
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
}
// 生产者接口,方法无参数有返回值
@FunctionalInterface
public interface Supplier<T> {
T get();
}
// 类实现 Runnable 接口时,必须重写 run 方法
@FunctionalInterface
public interface Runnable {
public abstract void run();