Reusing Lambda Expressions
Lambda expressions are deceivingly concise and it’s easy to carelessly duplicate them in code. Duplicate code leads to poor-quality code that’s hard to maintain; if we needed to make a change, we’d have to find and touch the relevant code in several places.
Avoiding duplication can also help improve performance. By keeping the code related to a piece of knowledge concentrated in one place, we can easily study its performance profile and make changes in one place to get better performance.
Now let’s see how easy it is to fall into the duplication trap when using lambda expressions, and also consider ways to avoid it.
Suppose we have a few collections of names: friends, editors, comrades, and so on.
| | final List<String> ... |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access