search

Home  >  Q&A  >  body text

java - JDK8 predicate function interface (Predicate) source code question?

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

迷茫迷茫2715 days ago956

reply all(4)I'll reply

  • 滿天的星座

    滿天的星座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);
    };

    reply
    0
  • 给我你的怀抱

    给我你的怀抱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.

    reply
    0
  • 某草草

    某草草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

    reply
    0
  • 三叔

    三叔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

    reply
    0
  • Cancelreply