Predicate接口

首先上源码

  • package java.util.function;   //为function包下
    
    import java.util.Objects;
    
    /**
     * Represents a predicate (boolean-valued function) of one argument. 
     // 代表了一个参数的谓语  (这里指的就是boolean值)
     *
     * 

    This is a functional interface 函数式接口 * whose functional method is {@link #test(Object)}. //函数式接口是test * * @param the type of the input to the predicate //输入的泛型 返回谓语 * * @since 1.8 //来自jdk1.8后 */ @FunctionalInterface public interface Predicate { /** * 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. * *

    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 and(Predicate 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 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. * *

    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 or(Predicate 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 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 Predicate 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 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  void PredicateTest(Predicate t, List list){
        list.forEach(l -> {
            if(t.test(l)) System.out.println(l.toString());
        });
    }

    public static  void PredicateTestByAnd(Predicatet, Predicate t2, List list){
        list.forEach(l -> {
            if(t.and(t2).test(l)) System.out.println(l.toString());
        });
    }
    public  static  void PredicateTestByNegate(Predicate t, List list){
        list.forEach(l -> {
            if(t.negate().test(l)) System.out.println(l.toString());
        });
    }

    public static  void PredicateTestByOr(Predicatet, Predicate t2, List list){
        list.forEach(l -> {
            if(t.or(t2).test(l)) System.out.println(l.toString());
        });
    }

    public static  void PredicateTestByEqual(List list){

            System.out.println(Predicate.isEqual("t").test("t"));

    }

}
  • 可能有人会问

    t.and(t2).test(l)为什么 会是这样的语句,但你可以看下源码,知道return的是Predicate,既我们最终要.test(l),并且除了test意外,其余都是default方法,这在Java8的时候是可以使用的,既直接在接口中实现

你可能感兴趣的:(Lambda,Java8)