Java 8 新特性:4-斷言(Predicate)介面

來源:互聯網
上載者:User

標籤:argument   eth   amp   with   code   array   isnull   將不   div   

(原)

這個介面主要用於判斷,先看看它的實現,說明,再給個例子。

/* * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.util.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 */@FunctionalInterfacepublic 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.     * 通過這個predicate和它的參數predicate 返回一個邏輯與的判斷結果, *當去計算這個複合的predicate時,如果當前的predicate 結果是false,那麼就不會計算它的參數other的值。     * <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.     * 返回一個predicate 代表了這個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.     *通過這個predicate和它的參數predicate 返回一個邏輯或的判斷結果,當計算這個組合的predicate,如果這個predicate是true ,那麼它的參數other將不會計算     * <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)}.     *如果二個參數機等的話,根據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);    }}

  

這裡其實慢慢看它的doc文檔,還真沒有直接看它的實現來的快。無非就是一個判斷的函數式介面,主要做邏輯與或非的判斷,其中還有一個靜態方法,其實現是這樣的:

 

return (null == targetRef)                ? Objects::isNull                : object -> targetRef.equals(object);

 

  

null == targetRef這個就不說了,因為它的返回結果是predicate,所以Objects::isNull必需是predicate的執行個體,它代表了一個方法的引用,為什麼它符合這個函數式介面的唯一抽象方法boolean test(T t);這個呢?我們進去看下它的實現。

 

public static boolean isNull(Object obj) {        return obj == null;} 

這是一個靜態方法引用,接收一個Object類型的參數,返回一個boolean類型,這完全附合這個函數式介面的boolean test(T t);抽象方法,那麼編譯器就會認為它是predicate這個函數式介面的一個實現。

 

下面給出一個例子,看下怎麼使用的,結果我就不分析了。

package com.demo.jdk8;import java.util.Arrays;import java.util.List;import java.util.function.Predicate;public class Test4 {public static void main(String[] args) {Predicate<String> p = s -> s.length() > 3;System.out.println(p.test("hello"));List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8);System.out.println("part1------------------");findOdd(list);System.out.println("part2------------------");conditionFilter(list, ppp -> ppp % 2 == 1);System.out.println("part3------------------");and(list, p1 -> p1 > 3, p2 -> p2 < 7);System.out.println("part4------------------");or(list,  p1 -> p1 > 3, p2 -> p2 % 2 == 1);System.out.println("part5------------------");negate(list, p1 -> p1 > 3);System.out.println("part6------------------");System.out.println(isEqual("abc").test("abcd"));}//找到集合中的奇數public static void findOdd(List<Integer> list){for (int i = 0; i < list.size(); i++) {if(list.get(i) % 2 == 1){System.out.println(list.get(i));}}}public static void conditionFilter(List<Integer> list,Predicate<Integer> p){for (int i = 0; i < list.size(); i++) {if(p.test(list.get(i))){System.out.println(list.get(i));}}}public static void and(List<Integer> list,Predicate<Integer> p1,Predicate<Integer> p2){for (int i = 0; i < list.size(); i++) {if(p1.and(p2).test(list.get(i))){System.out.println(list.get(i));}}}public static void or(List<Integer> list,Predicate<Integer> p1,Predicate<Integer> p2){for (int i = 0; i < list.size(); i++) {if(p1.or(p2).test(list.get(i))){System.out.println(list.get(i));}}}public static void negate(List<Integer> list,Predicate<Integer> p1){for (int i = 0; i < list.size(); i++) {if(p1.negate().test(list.get(i))){System.out.println(list.get(i));}}}public static Predicate isEqual(Object obj){return Predicate.isEqual(obj);}}

  

 

Java 8 新特性:4-斷言(Predicate)介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.