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? ? ?
滿天的星座2017-06-23 09:16:14
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:
return (t) -> {
return test(t) && other.test(t);
};
给我你的怀抱2017-06-23 09:16:14
returns not boolean
, but (t) -> { return true|false; }
, which means returning a functional interface
. What this functional interface
is depends on the context, as long as it accepts parameters. 1
and the return value is bool
is fine. In Predicate
it is the functional interface
itself, because the method stipulates that the return value is Predicate
.
某草草2017-06-23 09:16:14
@ 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
三叔2017-06-23 09:16:14
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 be test(t) && other.test(t)
It’s a whole, thank you@fabricatedbelief and @YaTou for your answer and Biancheng’s big reminder