The following code is given on p.35
final Function<String, Predicate<String>> startsWithLetter =
letter -> name -> name.startsWith(letter);
maybe it would be better to use parenthesis to highlight the right-associativity:
final Function<String, Predicate<String>> startsWithLetter =
letter -> (name -> name.startsWith(letter));
Underneath the Function
is used as follows:
final long countFriendsStartN =
friends.stream()
.filter(startsWithLetter.apply("N"))
.count();
The use if apply()
is not highlighted. Evidently this is the apply()
of the Function
interface and thus a shard of Java clunkiness - it just means “call this function with that argument”.
Why can’t I just write:
final long countFriendsStartN =
friends.stream()
.filter(startsWithLetter("N"))
.count();