(原)
這個接口主要用於判斷,先看看它的實現,說明,再給個例子。
/*
* 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
*/
@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,
* 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);
}
}
例子請看這里:https://github.com/LeeScofield/java8
