进阶day06-Predicate的or,negate方法

This commit is contained in:
2026-03-14 11:21:32 +08:00
parent 25c32a71e7
commit 1a273529c9
2 changed files with 58 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
package com.inmind.functional_interface02;
import java.util.function.Predicate;/*
Predicate的or方法
需求对一个字符串使用2个predicate判断它是否以h开头或者它的长度是否大于3
boolean test(T)
*/
public class Demo10 {
public static void main(String[] args) {
method( s-> s.startsWith("h"),
(String s)->{return s.length() >3;},
"el");
}
//定义一个方法接收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.or(predicate2);
boolean result = newPredicate.test(str);
System.out.println("最终结果:"+result);
}
}

View File

@@ -0,0 +1,27 @@
package com.inmind.functional_interface02;
import java.util.function.Predicate;
/*
Predicate的negate方法
需求对一个字符串使用1个predicate判断它是否以h开头,结果取反
boolean test(T)
*/
public class Demo11 {
public static void main(String[] args) {
method( s-> s.startsWith("h"),
"el");
}
//定义一个方法接收2个判断条件predicate1个要判断的字符串没有返回值
public static void method(Predicate<String> predicate1, String str) {
boolean result = predicate1.negate().test(str);
System.out.println("最终结果:"+result);
}
}