Which of the following methods of java.util.function.Predicate aredefault methods?
and(Predicate<? super T> other)
isEqual(Object targetRef)
negate()
not(Predicate<? super T> target)
or(Predicate<? super T> other)
test(T t)
Understanding java.util.function.Predicate<T>
The Predicate interface represents a function thattakes an input and returns a boolean(true or false).
It is often used for filtering operations in functional programming and streams.
Analyzing the Methods:
and(Predicate<? super T> other)→Default method
Combines two predicates usinglogical AND(&&).
java
Predicate startsWithA = s -> s.startsWith("A");
Predicate hasLength3 = s -> s.length() == 3;
Predicate combined = startsWithA.and(hasLength3);
âŒisEqual(Object targetRef)→Static method
Not a default method, because it doesnot operate on an instance.
Predicate isEqualToHello = Predicate.isEqual("Hello");
negate()→Default method
Negates a predicate (! operator).
Predicate notEmpty = s -> !s.isEmpty();
Predicate isEmpty = notEmpty.negate();
âŒnot(Predicate<? super T> target)→Static method (introduced in Java 11)
Not a default method, since it is static.
or(Predicate<? super T> other)→Default method
Combines two predicates usinglogical OR(||).
âŒtest(T t)→Abstract method
Not a default method, because every predicatemust implement this method.
Thus, the correct answers are:and(Predicate<? super T> other), negate(), or(Predicate<? super T> other)
References:
Java SE 21 - Predicate Interface
Java SE 21 - Functional Interfaces