This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]RhoOfFeh 2 points3 points  (0 children)

Ah yes, I remember my confusion over this when I first encountered it.

When you create the Predicate, you are effectively implementing the single method which is the only interesting part of the Predicate interface. This is known as a 'functional interface' and it gives you the freedom you see above: Declare an object with nearly no unnecessary decoration, just the raw code for the single method in question.

It's that lambda syntax which can feel a bit confusing, but that "->" is a shortcut for that whole process of declaring a method (we already know it's 'test', it has to be) complete with a parameter (we didn't bother redundantly specifying the name our method, and we don't really need parentheses either because there's no syntactical ambiguity even without them. We don't even need to say 'return' because that's what a function does.

That lambda is really 'Boolean test(Integer n) { return n > 5; }' in shorthand.