Predicate
源码:
@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.
*
* <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.
*
* @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.
*
* <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)}.
*
* @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);
}
}
test(T t)
根据给定的判断方法,判断入参是否满足条件,返回true/false
public class PredicateTest {
public static void main(String[] args) {
//即:用e->e.length()>5 实现test(T t)方法
Predicate<String> predicate=e->e.length()>5;
System.out.println(predicate.test("shiyaochang")); //true
}
}
public class PredicateTest {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9,10);
list=filter(list, e -> e % 2 == 0); //过滤出偶数
System.out.println(list);//[1, 3, 5, 7, 9]
}
private static List<Integer> filter(List<Integer> list, Predicate<Integer> predicate) {
ArrayList<Integer> L = new ArrayList<>();
for (Integer integer : list) {
boolean B = predicate.test(integer);
if (B) {
L.add(integer);
}
}
return L;
}
}
and
//参数是一个Predicate,返回值也是一个Predicate,返回逻辑与,即对于入参T而言,要同时满足参数other的test和本身的test
//即: 用(t) -> test(t) && other.test(t)实现了返回的Predicate的test()方法
default Predicate<T> and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}
栗子:
public class PredicateTest2 {
public static void main(String[] args) {
//即:用e->e.length()>5 实现test(T t)方法
Predicate<String> pre1 = e -> e.length() > 5;
Predicate<String> pre2= e -> e.length() < 7;
System.out.println(pre1.and(pre2).test("shiyaochang"));//false
System.out.println(pre1.and(pre2).test("shiyao"));//true
}
}
or
//满足本事的test或者入参other的test其一即可
default Predicate<T> or(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) || other.test(t);
}
栗子:
public class PredicateTest2 {
public static void main(String[] args) {
//即:用e->e.length()>5 实现test(T t)方法
Predicate<String> pre1 = e -> e.length() > 5;
Predicate<String> pre2= e -> e.length() < 7;
System.out.println(pre1.or(pre2).test("shiyaochang"));//true
System.out.println(pre1.or(pre2).test("shiyao"));//true
}
}
negate
返回相反值
栗子:
public class PredicateTest2 {
public static void main(String[] args) {
//即:用e->e.length()>5 实现test(T t)方法
Predicate<String> pre1 = e -> e.length() > 5;
System.out.println(pre1.negate().test("shiyaochang"));//false
System.out.println(pre1.negate().test("sh"));//true
}
}
isEqual
static <T> Predicate<T> isEqual(Object targetRef) {
return (null == targetRef)
? Objects::isNull
: object -> targetRef.equals(object);
// 返回值是一个Predicate
// 即用(null == targetRef)? Objects::isNull: object -> targetRef.equals(object)实现Predicate的test(),调用isEqual时判断targetRef是否为空,不为空则比较是否与test的入参object是否相等
}
栗子:
public class PredicateTest2 {
public static void main(String[] args) {
PredicateTest2 T2=new PredicateTest2();
System.out.println(T2.isEqualTest("shiyao").test("shiyao"));
System.out.println(T2.isEqualTest("shiyao").test("shiyaochang"));
System.out.println(T2.isEqualTest(null).test("shiyaochang"));
}
public Predicate<String> isEqualTest(Object str){
return Predicate.isEqual(str);
//Predicate.isEqual(str);会返回一个Predicate
//如果str不为null,则会用表达式object -> targetRef.equals(object)实现该Predicate的test();
}
}
给个饭钱?
- Post link: http://sovzn.github.io/2022/08/27/Predicate/
- Copyright Notice: All articles in this blog are licensed under unless otherwise stated.
若没有本文 Issue,您可以使用 Comment 模版新建。
GitHub Issues