I have a problem understanding Java Predicates...
Example:
public class UserPredicates {
public static Predicate<User> isNameEmpty() {
return p -> p.getFirstName().isEmpty() && p.getLastName().isEmpty();
}
}
The above example confusing to me, how does it know that p
have the method getFirstName()
and getLastName()
?
If I understand it correctly, isNameEmpty()
returns a function with one parameter ( p
), but does the compiler really figure out the type by looking at the return type?
And the returned function is test()
from the Predicate interface?
Predicate<User>
means a function that takes a User
as its parameter, and returns a boolean
.
See Javadoc :
Interface Predicate
...
Type Parameters:
T - the type of the input to the predicate
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.