进阶day06-Predicate的and方法
This commit is contained in:
@@ -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个判断条件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.and(predicate2);
|
||||
boolean result = newPredicate.test(str);
|
||||
|
||||
System.out.println("最终结果:"+result);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user