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 consider ways to avoid it.

Suppose we have a few collections of names: friends, editors, comrades, and so on.

 
final​ ​List​<​String ...

Get Functional Programming in Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.