Predicate接口是Function接口的表弟。和Function接口类似,Predicate接口也有两个方法。
看看接口定义:
public interface Predicate<T> {
boolean apply(T input)
boolean equals(Object object)
}
正如之前使用Function接口的例子,我们没有更细节的介绍equals方法。apply方法返回对输入应用Predicate的结果。Function接口用于转换对象,Predicate则是用于过滤对象。这个用于Predicates的指导方针同样也适用于Functions;不要在更简单的过程方法可以满足的情况下在使用Predicates。同样,Predicate函数不应该有任何的副作用。