* and(), or(), negate() 디폴트 메소드
- Predicate 함수적 인터페이스의 디폴트 메소드
- and() - &&와 대응 : 두 Predicate가 모두 true를 리턴하면 최종적으로 true 리턴
- or() - || 와 대응 : 두 Predicate 중 하나만 true를 리턴하면 최종적으로 true 리턴
- negate() - ! 와 대응 : Predicate의 결과가 true이면 false, false이면 true 리턴
public class PredicateAndOrNegateEx {
public static void main(String[] args) {
// 2의 배수를 검사
IntPredicate predicateA = a -> a % 2 == 0;
// 3의 배수를 검사
IntPredicate predicateB = a -> a % 3 == 0;
IntPredicate predicateAB;
boolean result;
// and()
predicateAB = predicateA.and(predicateB);
result = predicateAB.test(9);
System.out.println("9는 2와 3의 배수 입니까? " + result);
// or()
predicateAB = predicateA.or(predicateB);
result = predicateAB.test(9);
System.out.println("9는 2 또는 3의 배수 입니까? " + result);
// negate()
predicateAB = predicateA.negate();
result = predicateAB.test(9);
System.out.println("9는 홀수입니까? " + result);
}
}
* isEqual() 정적 메소드
- Predicate<T>의 정적 메소드
public class PridicateIsEqualEx {
public static void main(String[] args) {
Predicate<String> predicate;
predicate = Predicate.isEqual(null);
System.out.println("null, null : " + predicate.test(null));
predicate = Predicate.isEqual("Java");
System.out.println("null, Java : " + predicate.test(null));
predicate = Predicate.isEqual("Java");
System.out.println("Java, Java : " + predicate.test("Java"));
}
}