import java.util.function.Predicate;
public class Main {
public static void main(String[] args) {
Predicate<Integer> isEven = n -> n % 2 == 0;
Predicate<Integer> isPositive = n -> n > 0;
System.out.println(isEven.and(isPositive).test(-4));
System.out.println(isEven.or(isPositive).test(-4));
}
}
選択肢
- あなたの回答
- 正解
正解
C. false / true
詳細解説
-4 は偶数なので isEven は true、負なので isPositive は false です。and は両方 true のとき true なので false、or は片方でも true なら true なので true が表示されます。
なぜ他の選択肢が違うのか
- -4 は偶数なので isEven は true です。and は全体として false になります。
- and では isPositive が false のため全体も false になります。
- -4 は偶数なので or では true になります。
罠ポイント
Predicate には and / or / negate の合成メソッドがあります。and は &&、or は || に対応します。