进阶day06-Predicate的and方法

This commit is contained in:
2026-03-14 11:04:24 +08:00
parent 20932dcfcb
commit 25c32a71e7

View File

@@ -0,0 +1,40 @@
package com.inmind.functional_interface02;
import java.util.function.Predicate;
/*
Predicate的and方法
default Predicate<T> and(Predicate<? super T> other)
返回一个组合的谓词表示该谓词与另一个谓词的短路逻辑AND &&。
需求对一个字符串使用2个predicate判断它是否以h开头它的长度是否大于3
boolean test(T)
*/
public class Demo09 {
public static void main(String[] args) {
/*method((String s)->{return s.startsWith("h");},
(String s)->{return s.length() >3;},
"helloworld");*/
method( s-> s.startsWith("h"),
(String s)->{return s.length() >3;},
"helloworld");
}
//定义一个方法接收2个判断条件predicate1个要判断的字符串没有返回值
public static void method(Predicate<String> predicate1, Predicate<String> predicate2, String str) {
/*boolean result1 = predicate1.test(str);
boolean result2 = predicate2.test(str);
boolean result = result1&&result2;*/
// boolean result = predicate1.test(str)&&predicate2.test(str);
Predicate<String> newPredicate = predicate1.and(predicate2);
boolean result = newPredicate.test(str);
System.out.println("最终结果:"+result);
}
}