版本
JDK8(JDK1.8)
Predicate接口源码重点
1.Predicate(谓词)是一个函数式接口,即接口里只一一个抽象方法 boolean test(T t),其他都是默认方法和静态方法
2.实现Predicate的实现类可以重写text(T t)方法定义自己的规则,传入元素t符合自己的规则返回true,否则返回false
3.Predicate接口定义的部分方法
方法名 | 作用 |
---|---|
boolean test(T t) | 判断参数是否该规则 |
Predicate and(Predicate<? super T> other) | 返回一种组合谓词,表示该谓词和other谓词的短路逻辑AND |
Predicate or(Predicate<? super T> other) | 返回一种组合谓词,表示该谓词和other谓词的短路逻辑OR |
Predicate negate() | 表示该谓词的逻辑否定的谓词 |
Predicate接口源码
package java.util.function; import java.util.Objects; /** * Represents a predicate (boolean-valued function) of one argument. * 表示一个参数的谓词(布尔值函数)。 (谓词代表一种规则) * <p>This is a <a href="package-summary.html">functional interface</a> * whose functional method is {@link #test(Object)}. * 这是一个函数接口,其函数方法是test(Object)。 * @param <T> the type of the input to the predicate 谓词的输入类型 * * @since 1.8 */ @FunctionalInterface public interface Predicate<T> { /** * Evaluates this predicate on the given argument. 对给定参数计算此谓词。(即判断参数符不符合该规则) * * @param t the input argument 输入参数 * @return {@code true} if the input argument matches the predicate, * otherwise {@code false} 如果输入参数与谓词匹配,则为true,否则为false */ boolean test(T t); /** * Returns a composed predicate that represents a short-circuiting logical * AND of this predicate and another. When evaluating the composed * predicate, if this predicate is {@code false}, then the {@code other} * predicate is not evaluated. * 返回一个组合谓词,该谓词表示此谓词和另一个谓词的短路逻辑AND。 * 计算组合谓词时,如果此谓词为false,则不计算other谓词。 * * * <p>Any exceptions thrown during evaluation of either predicate are relayed * to the caller; if evaluation of this predicate throws an exception, the * {@code other} predicate will not be evaluated. * 在对任一谓词求值期间抛出的任何异常都将转发给调用方; * 如果对该谓词的求值引发异常,other谓词将不会求值。 * * @param other a predicate that will be logically-ANDed with this * predicate 将与此谓词逻辑AND的谓词 * @return a composed predicate that represents the short-circuiting logical * AND of this predicate and the {@code other} predicate * 返回一种组合谓词,表示该谓词和other谓词的短路逻辑AND * 实质是用两个Predicate的test()方法使用逻辑运算符组合在一起 * @throws NullPointerException if other is null */ default Predicate<T> and(Predicate<? super T> other) { // 判断other是否为空,是抛出异常 Objects.requireNonNull(other); return (t) -> test(t) && other.test(t); } /** * Returns a predicate that represents the logical negation of this * predicate. 返回表示此谓词逻辑否定的谓词。 * * @return a predicate that represents the logical negation of this * predicate 表示该谓词的逻辑否定的谓词 */ default Predicate<T> negate() { return (t) -> !test(t); } /** * Returns a composed predicate that represents a short-circuiting logical * OR of this predicate and another. When evaluating the composed * predicate, if this predicate is {@code true}, then the {@code other} * predicate is not evaluated. * 返回表示此谓词和另一个谓词的短路逻辑OR的组合谓词。 * 计算组合谓词时,如果此谓词为true,则不计算other谓词。 * * <p>Any exceptions thrown during evaluation of either predicate are relayed * to the caller; if evaluation of this predicate throws an exception, the * {@code other} predicate will not be evaluated. * 在对任一谓词求值期间抛出的任何异常都将转发给调用方; * 如果对该谓词的求值引发异常,other谓词将不会求值。 * * @param other a predicate that will be logically-ORed with this * predicate 将与此谓词进行逻辑或运算的谓词 * @return a composed predicate that represents the short-circuiting logical * OR of this predicate and the {@code other} predicate * 表示该谓词和ther谓词的短路逻辑OR的组合谓词 * @throws NullPointerException if other is null */ default Predicate<T> or(Predicate<? super T> other) { Objects.requireNonNull(other); return (t) -> test(t) || other.test(t); } /** * Returns a predicate that tests if two arguments are equal according * to {@link Objects#equals(Object, Object)}. * 返回一个谓词,该谓词根据Objects.equals(Object,Object)测试两个参数是否相等。 * * * @param <T> the type of arguments to the predicate 谓词的参数类型 * @param targetRef the object reference with which to compare for equality, * which may be {@code null} * 用于比较相等性的对象引用,该引用可以是null * @return a predicate that tests if two arguments are equal according * to {@link Objects#equals(Object, Object)} * 根据Objects.equals(Object,Object)测试两个参数是否相等的谓词 */ static <T> Predicate<T> isEqual(Object targetRef) { return (null == targetRef) ? Objects::isNull : object -> targetRef.equals(object); } /** * Returns a predicate that is the negation of the supplied predicate. * This is accomplished by returning result of the calling * {@code target.negate()}. * 返回对提供的谓词求反的谓词。这是通过返回调用target.negate()的结果来实现的。 * @param <T> the type of arguments to the specified predicate * 指定谓词的参数类型 * @param target predicate to negate 否定谓词 * * @return a predicate that negates the results of the supplied * predicate 对提供的谓词的结果求反的谓词 * * @throws NullPointerException if target is null 如果目标为空 * * @since 11 */ @SuppressWarnings("unchecked") static <T> Predicate<T> not(Predicate<? super T> target) { Objects.requireNonNull(target); return (Predicate<T>)target.negate(); } }