Java Reference
In-Depth Information
2.3.3. Sixth attempt: using a lambda expression
The previous code can be rewritten as follows in Java 8 using a lambda expression:
List<Apple> result =
filterApples(inventory, (Apple apple) -> "red".equals(apple.getColor()) );
You have to admit this code looks a lot cleaner than our previous attempts! It's great because it's
starting to look a lot closer to the problem statement. We've now tackled the verbosity issue.
Figure 2.4 summarizes our journey so far.
Figure 2.4. Behavior parameterization vs. value parameterization
2.3.4. Seventh attempt: abstracting over List type
There's one more step that you can do in your journey toward abstraction. At the moment, the
filterApples method works only for Apple. But you can also abstract on the List type to go
beyond the problem domain you're thinking of right now:
 
Search WWH ::




Custom Search