Original
This interface is mainly used for judging, first look at its implementation, description, and then give an example.
/* Copyright (c), Oracle and/or its affiliates. All rights reserved. * ORACLE proprietary/confidential. Use are subject to license terms. */package Java.util.function;import java.util.objects;/** * represents a predicate (boolean-valued function) of one Argum Ent. * Represents a Boolean based assertion according to a parameter * <p>this is a <a href= "package-summary.html" >functional interface</a> * Whose functional method is {@link #test (Object)}. * This is a functional interface, and its function method is test * @param <T> the type of the input to the predicate * Gets an assertion based on the input type * @since 1.8 */@FunctionalI Nterfacepublic interface Predicate<t> {/** * evaluates this predicate on the given argument. * The result of judging according to the given parameters * @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 the This predicate and another. When EvaluatinG The composed * predicate, if this predicate are {@code false}, then the {@code other} * predicate are not evaluate D. * Through this predicate and its parameters predicate returns a logical and a judgment result, * when to calculate this compound predicate, if the current predicate result is false, then it does not calculate the value of its parameter other. * <p>any exceptions thrown during evaluation of either predicate is relayed * to the caller; If evaluation of this predicate throws a exception, the * {@code other} predicate won't be evaluated. * If any of these two throws an exception, the specific processing is given to the caller, and if an exception is thrown, it will not be executed. * @param other a predicate that'll be logically-anded with this * predicate * @return a composed PR Edicate that represents the short-circuiting logical * and of this predicate and the {@code other} predicate * @th Rows 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. * Return a predicate represents the logic of this predicate non * @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 anot Her. When evaluating the composed * predicate, if this predicate are {@code true}, then the {@code other} * predicate are Not evaluated. * by this predicate and its parameters predicate returns a logical or judgment result, when calculating this combination of predicate, if this predicate is true then its parameters other will not be calculated * <p>any Exceptions thrown during evaluation of either predicate is relayed * to the caller; If evaluation of this predicate throws a exception, the * {@code other} predicate won't be evaluated. * If any of these two throws an exception, the specific processing is given to the caller, and if an exception is thrown, it will not be executed. * @param other a predicate that'll be logically-ored with this * predicate * @return A composed predicate that represents the short-circuiting logical * OR of this predicate a nd the {@code other} predicate * @throws nullpointerexception if and 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 and arguments is equal according * to {@link objects#equals (Object, Object)}. * If two parameters and so on, according to Objects#equals (object, object) returns the result of an assertion * @param <T> the type of arguments to the predicate * @param targetref the object reference with which to compare for equality, * which could be {@code null} * @return a predicate that tests if arguments is equal according * to {@link Objects#equals (object, Object)} */Static <T> predicate<t> isequal (Object targetref) {return (null = = Targetref)? ObjEcts::isnull:object-Targetref.equals (object); }}
here in fact slowly look at its Doc document, and really did not directly see its implementation to come fast. Is nothing more than a functional interface of judgment, mainly to do logic and or non-judgment, there is a static method, its implementation is this:
return (NULL = = Targetref) ? Objects::isnull : Object---Targetref.equals (object);
Null = = Targetref This is not said, because its return result is predicate, so objects::isnull must be an instance of predicate, which represents a reference to a method, and why it conforms to the unique abstract method of this functional interface, boolean test (T-t); What about this one? Let's go in and see how it's going to come true.
public static Boolean isNull (Object obj) { return obj = = null;}
This is a static method reference that receives a an argument of type Object that returns a Boolean type that completely fits the Boolean test (T-T) of this functional interface ; abstract method, then the compiler will think it is predicate An implementation of this function-type interface.
Here is an example to see how to use, the result I do not analyze.
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 < 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")); Locate the odd public in the collection, 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 new features: 4-assertion (predicate) interface