Getting Started with Lambda Expressions

Getting Started with Lambda Expressions

When Java 8 was released in 2014, several new features geared towards increasing developer productivity were introduced along with it. The two most prominent enhancements were Lambda Expressions and Streams API. In tandem, these features offer an effective new way to approach Collections processing. In this post, I’ll cover lambda expressions, but be on the lookout for a future post covering the Streams API.

The Basics of Lambda

Lambda expressions, at their core, are a means to convey an entire method as a parameter. Java approached method parameterization by allowing you to pass around functional interface implementations in a concise manner. A functional interface contains a single abstract method that defines its overall purpose. You may be familiar with some interfaces that are already structured this way, such as Runnable. Lambda expressions represent the shorthand way of implementing these functional interfaces without having to verbosely override the interface every time.

Your First Lambda Expression

Let’s take a look at the simplified source code for one functional interface, Consumer, and then apply it concretely with a lambda expression.

Here’s an example lambda expression that implements Consumer:

Some initial observations:

If you are familiar with the Groovy programming language, you might notice the similarities between Java lambda expressions and Groovy closures. An equivalent Groovy closure to the lambda expression above is:

Indeed, both lambda expressions and closures exist to allow behavior parameterization. If your current project will not allow the use of Groovy, you can still emulate the advantages of Groovy closures by using Java lambda expressions.

Improving Your Existing Code

The utility and flexibility of lambda expressions become more apparent every time you use them; imagine you have multiple methods that work with a single file:

The File method examples above contain some boilerplate preparatory code; we repeatedly verify that the file is not a null reference and actually exists in the file system. It would be nice to have a single method with the preparatory code that accepts custom behavior as a parameter. If you could do this, you wouldn’t violate the “Don’t Repeat Yourself” principle so often. Every time you create another File method like those above with only slightly differing functionality, the complexity and repetition in your code grows.

With lambda expressions, you can parameterize the custom portions of these methods. To do this, we create the following method and use the Consumer functional interface:

Now you can remove the other methods and perform virtually any action you want with the behaviorally parameterized method, supplying a lambda expression for the Consumer parameter:

Taking Things Further

There are many more functional interfaces that Java 8 supplies other than Consumer:

The entire list of functional interfaces available in Java 8 can be found here.

Lambda expressions showcase Java’s evolution to accommodate functional programming. The ability to be able to pass methods as easily as objects can broaden your design options, especially when requirements can change on a whim. While behavior parameterization was an option before Java 8 with anonymous implementations of classes, it was verbose and often too specialized. Java 8 functional interfaces and lambda expressions are excellent additions to your repertoire for handling common tasks in a concise and flexible manner.