Foundation concepts to be familiar with

Luis Santiago
Dec 24, 2017 · 3 min read
Photo by Goran Ivos on Unsplash

Before Lambda expressions support was added by JDK 8, I’d only used examples of them in languages like C# and C++.

Once this feature was added to Java, I started looking into them a bit closer.

The addition of lambda expressions adds syntax elements that increase the expressive power of Java. In this article, I want to focus on foundation concepts you need to be familiar with so you can start adding lambda expressions to your code today.

Quick introduction

Lambda expressions take advantage of parallel process capabilities of multi-core environments as seen with the support of pipeline operations on data in the Stream API.

They are anonymous methods (methods without names) used to implement a method defined by a functional interface. It’s important to know what a functional interface is before getting your hands dirty with lambda expressions.

Functional interface

A functional interface is an interface that contains one and only one abstract method.

If you take a look at the definition of the Java standard Runnable interface, you will notice how it falls into the definition of functional interface because it only defines one method: run().

In the code sample below, the method computeName is implicitly abstract and is the only method defined, making MyName a functional interface.

Functional Interface

The arrow operator

Lambda expressions introduce the new arrow operator -> into Java. It divides the lambda expressions in two parts:

(n) -> n*n

The left side specifies the parameters required by the expression, which could also be empty if no parameters are required.

The right side is the lambda body which specifies the actions of the lambda expression. It might be helpful to think about this operator as “becomes.” For example, “n becomes n*n,” or “n becomes n squared.”

With functional interface and arrow operator concepts in mind, you can put together a simple lambda expression:

Numeric Test
Greeting Lambda

The variables morningGreeting and eveningGreeting, lines 6 and 7 in the sample above, make a reference to MyGreeting interface and define different greeting expressions.

When writing a lambda expression, it is also possible to explicitly specify the type of the parameter in the expression like this:

Lambda with Type

Block lambda expressions

So far, I have covered samples of single expression lambdas. There is another type of expression used when the code on the right side of the arrow operator contains more than one statement known as block lambdas:

Block Lambda

Generic functional interfaces

A lambda expression cannot be generic. But the functional interface associated with a lambda expression can. It is possible to write one generic interface and handle different return types like this:

Generic Functional Interface

Lambda expressions as arguments

One common use of lambdas is to pass them as arguments.

They can be used in any piece of code that provides a target type. I find this exciting, as it lets me pass executable code as arguments to methods.

To pass lambda expressions as parameters, just make sure the functional interface type is compatible with the required parameter.

Lambda Expression as an Argument

These concepts will give you a good foundation to start working with lambda expressions. Take a look at your code and see where you can increase the expressive power of Java.

Better Programming

Advice for programmers.

Luis Santiago

Written by

Software Engineer living in Miami. I am passionate about coding, growth mindset, and fitness. You can also find me exploring new countries and restaurants.

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade