java - JDK8 predicate function interface (Predicate) source code question?
迷茫
迷茫 2017-06-23 09:14:14
0
4
950

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? ? ?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(4)
滿天的星座

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);
};
给我你的怀抱

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.

某草草

@ 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 be test(t) && other.test(t) It’s a whole, thank you@fabricatedbelief and @YaTou for your answer and Biancheng’s big reminder

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template