天天看點

Predicate接口

首先上源碼

  • 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" target="_blank" rel="external nofollow" >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的時候是可以使用的,既直接在接口中實作      

繼續閱讀