首先上源码
-
package java.util.function; //为function包下 import java.util.Objects; /** * Represents a predicate (boolean-valued function) of one argument. // 代表了一个参数的谓语 (这里指的就是boolean值) * * <p>This is a <a href="package-summary.html">functional interface</a> 函数式接口 * whose functional method is {@link #test(Object)}. //函数式接口是test * * @param <T> the type of the input to the predicate //输入的泛型 返回谓语 * * @since 1.8 //来自jdk1.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, //这里的意思是 返回 是否和参数匹配的 若匹配即返回true 否则 false * otherwise {@code false} */ boolean test(T t); /** * Returns a composed predicate that represents a short-circuiting logical * AND of this predicate and another. //返回一个使用AND关键字 组合的判断 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 //paramter other 参数不能为空 */ 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. //返回一个相反逻辑的boolean值 * * @return a predicate that represents the logical negation of this * predicate */ default Predicate<T> negate() { return (t) -> !test(t); } /** 这边跟上边的AND是一个道理,即返回逻辑OR的语句 * 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); } /** 判断两者是否相等,使用equal函数 * 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); } }
这里的注释该解释的都有,下面附上测试类
package com.pjh.Predicate;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class test {
public static void main(String[] args) {
Integer[] array = new Integer[]{5,6,7,8,10,20,50,90,101,100,5};
List<Integer> list = Arrays.asList(array);
/* //测试1 boolean test(T t);, 下面的lambda函数主体只有一行 因此省略了{} 筛选大于50的数字
PredicateTest(l -> l > 50, list);*/
/* PredicateTestByAnd(l -> l > 50, l -> l % 2== 0, list);
筛选大于50的数字 并且为偶数
*/
/* PredicateTestByNegate(l -> l > 50, list);
筛选小于50的数字
*/
/* PredicateTestByOr(l -> l > 50, l -> l % 2== 0, list);
筛选大于50的数字 或为偶数
*/
/* PredicateTestByEqual(list);
判断字符串是否相等 ,个人认为 这功能挺鸡肋 - -
*/
}
public static <T> void PredicateTest(Predicate<T> t, List<T> list){
list.forEach(l -> {
if(t.test(l)) System.out.println(l.toString());
});
}
public static <T> void PredicateTestByAnd(Predicate<T>t, Predicate<T> t2, List<T> list){
list.forEach(l -> {
if(t.and(t2).test(l)) System.out.println(l.toString());
});
}
public static <T> void PredicateTestByNegate(Predicate<T> t, List<T> list){
list.forEach(l -> {
if(t.negate().test(l)) System.out.println(l.toString());
});
}
public static <T> void PredicateTestByOr(Predicate<T>t, Predicate<T> t2, List<T> list){
list.forEach(l -> {
if(t.or(t2).test(l)) System.out.println(l.toString());
});
}
public static <T> void PredicateTestByEqual(List<T> list){
System.out.println(Predicate.isEqual("t").test("t"));
}
}
-
可能有人会问
t.and(t2).test(l)为什么 会是这样的语句,但你可以看下源码,知道return的是Predicate,既我们最终要.test(l),并且除了test意外,其余都是default方法,这在Java8的时候是可以使用的,既直接在接口中实现