s-day07- Predicate的and-or-negate方法介绍
This commit is contained in:
@@ -0,0 +1,44 @@
|
|||||||
|
package com.inmind.funcitonal_predicate05;
|
||||||
|
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
/*
|
||||||
|
2.Predicate里面的and方法
|
||||||
|
|
||||||
|
需求:对一个字符串,使用2个predicate,判断它是否以h开头,它的长度是否大于3
|
||||||
|
分析:
|
||||||
|
1.一个predicate要对字符串进行判断是否以h开头
|
||||||
|
2.另一个predicate要对字符串进行判断它的长度是否大于3
|
||||||
|
*/
|
||||||
|
public class Demo10 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
method1(
|
||||||
|
s->s.startsWith("h"),
|
||||||
|
s->s.length()>3,
|
||||||
|
"hello java"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void method1(Predicate<String> predicate1, Predicate<String> predicate2, String s) {
|
||||||
|
/*boolean result1 = predicate1.test(s);
|
||||||
|
boolean result2 = predicate2.test(s);
|
||||||
|
boolean result = result1 && result2;*/
|
||||||
|
|
||||||
|
boolean result = predicate1.and(predicate2).test(s);
|
||||||
|
System.out.println(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
//需求:对一个字符串,使用2个predicate,判断它是否以h开头或者它的长度是否大于3
|
||||||
|
public static void method2(Predicate<String> predicate1, Predicate<String> predicate2, String s) {
|
||||||
|
/*boolean result1 = predicate1.test(s);
|
||||||
|
boolean result2 = predicate2.test(s);
|
||||||
|
boolean result = result1 || result2;*/
|
||||||
|
|
||||||
|
boolean result = predicate1.or(predicate2).test(s);
|
||||||
|
System.out.println(result);
|
||||||
|
// boolean value = predicate1.negate().test(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user