一、源码
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)}.
*
* @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}
*/
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.
*
* <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.
*
* @param other a predicate that will be logically-ANDed with this
* predicate
* @return a composed predicate that represents the short-circuiting logical
* AND of this predicate and the {@code other} predicate
* @throws NullPointerException if other is null
*/
default Predicate<T> and(Predicate<? super T> 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.
*
* <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.
*
* @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
* @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)}.
*
* @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}
* @return a predicate that tests if two arguments are equal according
* to {@link Objects#equals(Object, Object)}
*/
static <T> Predicate<T> isEqual(Object targetRef) {
return (null == targetRef)
? Objects::isNull
: object -> targetRef.equals(object);
}
}
二、解析
Predicate是Java中一个函数式编程体现,是Java8出来的。
表示一个参数的谓词(布尔值函数)。这是一个功能接口,其功能方法是test(Object)。
1.test()
评估参数里面的表达式(验证传进来的参数符不符合规则);它的返回值是一个boolean类型。
2.and()
返回组合谓词,该谓词表示此谓词和另一个谓词的短路逻辑与。在计算复合谓词时,如果该谓词为false,则不计算其他谓词。
3.negate()
返回表示此谓词的逻辑否定的谓词。
4.or()
返回组合谓词,该谓词表示此谓词和另一个谓词的短路逻辑或。在计算复合谓词时,如果该谓词为真,则不计算其他谓词。
5.isEqual()
返回一个谓词,该谓词根据object测试两个参数是否相等。
三、实例
import com.google.common.collect.Lists;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class PredicateTest {
public static void main(String[] args) {
List<String> list = Lists.newArrayList("12345","Abc","Tom","Jacks","ide");
Predicate<String> predicate1 = str -> str.length() < 4;
Predicate<String> predicate2 = str -> str.startsWith("A");
List result1 = list.stream().filter(predicate1.and(predicate2)).collect(Collectors.toList());
System.out.println(result1.toString());
List result2 = list.stream().filter(predicate1.or(predicate2)).collect(Collectors.toList());
System.out.println(result2.toString());
List result3 = list.stream().filter(predicate1.and(predicate2.negate())).collect(Collectors.toList());
System.out.println(result3.toString());
List result4 = list.stream().filter(Predicate.isEqual(predicate1)).collect(Collectors.toList());
System.out.println(result4.toString());
}
}
运行结果:
[Abc]
[Abc, Tom, ide]
[Tom, ide]
[]