package LambdaTest.LambdaTest05;
import java.util.function.Predicate;
/**
* FileName: PredicateTest02
* Author: lps
* Date: 2022/4/8 13:07
* Sign:刘品水 Q:1944900433
*/
public class PredicateTest02 {
public static void main(String[] args) {
boolean b1 = checkString("lps", (s -> {
return s.length() > 5;
}));
System.out.println(b1);
boolean b2 = checkString("HelloWorld", s -> s.length() > 5);
System.out.println(b2);
System.out.println("==========");
boolean b3 = checkString("lpsjava", s -> s.length() > 4, s -> s.length() < 7);
System.out.println(b3);
boolean b4=checkStringOR("lpsjava",s -> s.length()>4,s -> s.length()<7);
System.out.println(b4);
}
private static boolean checkStringOR(String s, Predicate<String> pre1, Predicate<String> pre2) {
//default Predicate<T> or(Predicate<? super T> other)
//返回一个由谓词表示短路逻辑或该谓词和另一个。
// boolean b1 = pre1.test(s);
// boolean b2 = pre2.test(s);
// boolean b = b1 || b2;
// return b;
return pre1.or(pre2).test(s);
//default Predicate<T> or(Predicate<? super T> other) {
// Objects.requireNonNull(other);
// return (t) -> test(t) || other.test(t);
// }
}
//同一个字符串给出两个不同的判断条件 最后把这个
private static boolean checkString(String s, Predicate<String> pre1, Predicate<String> pre2) {
//default Predicate<T> or(Predicate<? super T> other)
//返回一个由谓词表示短路逻辑或该谓词和另一个。
// boolean b1 = pre1.test(s);
// boolean b2 = pre2.test(s);
// boolean b = b1 && b2;
// return b;
return pre1.and(pre2).test(s);
// default Predicate<T> and(Predicate<? super T> other) {
// Objects.requireNonNull(other);
// return (t) -> test(t) && other.test(t);
// }
}
private static boolean checkString(String s, Predicate<String> pre) {
return pre.test(s);
}
}
![]()
![]()