vwrynn
vwrynn

Reputation: 365

Understanding Java Predicates

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?

Upvotes: 0

Views: 172

Answers (1)

Jiri Tousek
Jiri Tousek

Reputation: 12440

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

Upvotes: 2

Related Questions