Notice
Recent Posts
Recent Comments
Link
SeouliteLab
[Java/자바] BiPredicate의 negate() 메소드 활용 예제 본문
BiPredicate의 negate() 메소드란?
BiPredicate 인터페이스에는 논리 연산을 반대로 하는 negate() 메소드가 있습니다. 이 메소드는 BiPredicate의 조건을 부정합니다.
예제 1: 두 숫자가 모두 양수가 아닌지 확인하기
import java.util.function.BiPredicate;
BiPredicate<Integer, Integer> isNotPositive = (a, b) -> a <= 0 || b <= 0;
BiPredicate<Integer, Integer> bothNotPositive = isNotPositive.negate();
boolean result1 = bothNotPositive.test(5, 7);
boolean result2 = bothNotPositive.test(4, -6);
System.out.println("Are both numbers not positive? " + result1); // 출력: false
System.out.println("Are both numbers not positive? " + result2); // 출력: true
이 예제에서는 negate() 메소드를 사용하여 두 숫자가 모두 양수가 아닌지 확인합니다.
예제 2: 두 문자열이 모두 "java"를 포함하지 않는지 확인하기
import java.util.function.BiPredicate;
BiPredicate<String, String> doesNotContainJava = (str1, str2) -> !str1.contains("java") && !str2.contains("java");
BiPredicate<String, String> bothDoNotContainJava = doesNotContainJava.negate();
boolean result1 = bothDoNotContainJava.test("java programming", "python and java");
boolean result2 = bothDoNotContainJava.test("python", "java script");
System.out.println("Do both strings not contain 'java'? " + result1); // 출력: false
System.out.println("Do both strings not contain 'java'? " + result2); // 출력: true
이 예제에서는 negate() 메소드를 사용하여 두 문자열이 모두 "java"를 포함하지 않는지 확인합니다.
예제 3: 두 숫자 중 하나가 10 미만이거나 다른 하나가 100 초과인지 확인하기
import java.util.function.BiPredicate;
BiPredicate<Integer, Integer> isTenOrLess = (a, b) -> a < 10 || b < 10;
BiPredicate<Integer, Integer> isHundredOrMore = (a, b) -> a > 100 || b > 100;
BiPredicate<Integer, Integer> tenOrLessOrHundredOrMore = isTenOrLess.or(isHundredOrMore);
BiPredicate<Integer, Integer> neitherTenOrLessNorHundredOrMore = tenOrLessOrHundredOrMore.negate();
boolean result1 = neitherTenOrLessNorHundredOrMore.test(5, 120);
boolean result2 = neitherTenOrLessNorHundredOrMore.test(15, 90);
System.out.println("Is one number neither 10 or less nor 100 or more? " + result1); // 출력: false
System.out.println("Is one number neither 10 or less nor 100 or more? " + result2); // 출력: false
이 예제에서는 negate() 메소드를 사용하여 두 숫자 중 하나가 10 미만이거나 다른 하나가 100 초과인지 확인합니다.
'프로그래밍' 카테고리의 다른 글
[Java/자바] UnaryOperator와 andThen() 메서드 활용 예제 (0) | 2024.03.13 |
---|---|
[Java/자바] UnaryOperator 인터페이스: 함수형 프로그래밍에서의 활용 (0) | 2024.03.13 |
[Java/자바] BiPredicate의 and() 메소드 활용 예제 (0) | 2024.03.13 |
[Java/자바] BiPredicate의 or() 메소드 활용 예제 (0) | 2024.03.13 |
[Java/자바] BiPredicate 인터페이스: 예제와 활용 (0) | 2024.03.13 |