package com.chunzhi.Test06Predicate;
import java.util.function.Predicate;
/*
逻辑表达式:可以连接多个判断的条件
&&:与运算符,有false则false
||:或运算符,有true则true
!:非(取反)运算符,非真则假,非假则真
Predicate接口中有一个方法and,表示并且关系,也可以用于连接两个判断条件
default Predicate<T> and(Predicate<? super T> other) {
Object.requireNonNull(other);
return (t) -> this.test(t) && other.test(t);
}
方法内部的两个判断条件,也是使用&&运算符连接起来的
*/
public class Test02Predicate_and {
/*
定义一个方法,方法的参数,传递一个字符串
传递两个Predicate接口
一个用于判断字符串的长度是否大于5
一个用于判断字符出中是否包含a
两个条件必须同时满足
*/
public static boolean checkString(String s, Predicate<String> pre1, Predicate<String> pre2) {
return pre1.and(pre2).test(s); // 等同于 return pre1.test(s) && pre2.test(s);
}
public static void main(String[] args) {
// 定义一个字符串
String s = "aoeywu";
// 调用checkString方法,参数传递字符串和两个Lambda表达式
boolean b =checkString(s, (String str) -> {
// 判断字符串长度是否大于5
return str.length() > 5;
}, (String str) -> {
// 判断字符串中是否含有a
return str.contains("a");
});
System.out.println(b);
}
}