There is a and()
method in the function interface, the source code is as follows
default Predicate<T> and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}
In addition, test()
The source code is as follows
boolean test(T t);
The question is why logical operations between &&
and boolean
type values can return a predicate object? ? ?
I don’t know what a predicate object is, but
test(t) && other.test(t)
This is an ordinary boolean expression, and it returns a boolean value, not a "predicate object". The entire return statement is actually the abbreviation of the following statement:returns not
boolean
, but(t) -> { return true|false; }
, which means returning afunctional interface
. What thisfunctional interface
is depends on the context, as long as it accepts parameters.1
and the return value isbool
is fine. InPredicate
it is thefunctional interface
itself, because the method stipulates that the return value isPredicate
.@ fabricated belief and @YaTou both said that what is returned is a Lambda, which meets the definition of
Predicate<T>
.I just want to say, why is Predicate translated as "predicate"? Although it has the meaning of "predicate", its other meaning is used here, "assertion, assertion (it is easier to understand using assertion in natural language, but Development technical books are generally called assertions)", which is used to judge something and get a true or false result - that is, it is judged to be true or it is judged to be false
It is true that there is no problem with the source code. I am in charge now and regard
(t) -> test(t)
as a whole. In fact, it should betest(t) && other.test(t)
It’s a whole, thank you@fabricatedbelief and @YaTou for your answer and Biancheng’s big reminder