Predicate接口

概述:
 函数式接口,有时候我们需要对某种类型的数据进行判断,从而得到一个boolean值结果。这时可以使用java.util.function.Predicate接口。

方法:

  • 抽象方法: test用于条件判断
 boolean test(T t);
  • 默认方法: and用于要将两个Predicate条件使用“与”逻辑连接起来实现“并且”的效果时
 default Predicate<T> and(Predicate<? super T> other) {
    Objects.requireNonNull(other);
    return (t) -> test(t) && other.test(t);
}
  • 默认方法:or实现逻辑关系中的“或”
default Predicate<T> or(Predicate<? super T> other) {
    Objects.requireNonNull(other);
    return (t) -> test(t) || other.test(t);
}
  • 默认方法: negate 实现逻辑关系中的“非”(取反)
default Predicate<T> negate() {
    return (t) -> !test(t);
}

你可能感兴趣的:(新特性)