O'Reilly logo

Functional Programming in Java by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Lexical Scoping and Closures

There’s a misconception among some developers that using lambda expressions may introduce duplication and lower code quality. Contrary to that belief, even when the code gets more complicated we still don’t need to compromise code quality to enjoy the conciseness that lambda expressions give, as we’ll see in this section.

We managed to reuse the lambda expression in the previous example; however, duplication will sneak in quickly when we bring in another letter to match. Let’s explore the problem further and then solve it using lexical scoping and closures.

Duplication in Lambda Expressions

Let’s pick the names that start with N or B from the friends collection of names. Continuing with the previous example, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required