Java Reference
In-Depth Information
And to use this, you call either
filterApples(inventory, Apple::isGreenApple );
or
filterApples(inventory, Apple::isHeavyApple );
We explain how this works in detail in the next two chapters. The key idea to take away for now
is that you can pass around a method in Java 8!
What's a Predicate?
The previous code passed a method Apple::isGreenApple (which takes an Apple for argument
and returns a boolean) to filterApples, which expected a Predicate-<Apple> parameter. The
word predicate is often used in mathematics to mean something function-like that takes a value
for an argument and returns true or false. As you'll see later, Java 8 would also allow you to
write Function<Apple,Boolean>—more familiar to readers who learned about functions but not
predicates at school—but using Predicate<Apple> is more standard (and slightly more efficient
because it avoids boxing a boolean into a Boolean).
Search WWH ::




Custom Search