进阶day06-Predicate的or,negate方法
This commit is contained in:
@@ -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个判断条件predicate,1个要判断的字符串,没有返回值
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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个判断条件predicate,1个要判断的字符串,没有返回值
|
||||
public static void method(Predicate<String> predicate1, String str) {
|
||||
|
||||
boolean result = predicate1.negate().test(str);
|
||||
System.out.println("最终结果:"+result);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user