Java Reference
In-Depth Information
You must admit that the code looks clearer! Don't worry if all the parts of the lambda expression
don't make sense yet; we explain all the pieces soon. For now, note that you're literally passing
only the code that's really needed to compare two apples using their weight. It looks like you're
just passing the body of the method compare. You'll learn soon that you can simplify your code
even more. We explain in the next section exactly where and how you can use lambda
expressions.
The lambda we just showed you has three parts, as shown in figure 3.1 :
Figure 3.1. A lambda expression is composed of parameters, an arrow,
and a body.
A list of parameters In this case it mirrors the parameters of the compare method of a
Comparator —two Apple s.
An arrow The arrow -> separates the list of parameters from the body of the lambda.
The body of the lambda Compare two Apple s using their weights. The expression is considered
the lambda's return value.
To illustrate further, the following listing shows five examples of valid lambda expressions in
Java 8.
Listing 3.1. Valid lambda expressions in Java 8
 
Search WWH ::




Custom Search