源码展示

package java.util.function;

import java.util.Objects;

/**
* Represents a predicate (boolean-valued function) of one argument.
*/
@FunctionalInterface
public interface Predicate<T> {

/**
* Evaluates this predicate on the given argument.
*/
boolean test(T t);

/**
* Returns a composed predicate that represents a short-circuiting logical
* AND of this predicate and another.
*/
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.
*/
default Predicate<T> negate() {
return (t) -> !test(t);
}

/**
* Returns a composed predicate that represents a short-circuiting logical
* OR of this predicate and another.
*/
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 Objects#equals().
*/
static <T> Predicate<T> isEqual(Object targetRef) {
return (null == targetRef)
? Objects::isNull
: object -> targetRef.equals(object);
}
}


示例

import java.util.Objects;
import java.util.function.Predicate;

public class Test {
public static void main(String args[]) {
Predicate<String > p = (str)->{return Objects.equals("费哥哥", str);};
System.out.println(p.test("费哥哥")); //true
}
}