Member-only story
How to Start Working With Lambda Expressions in Java
Foundation concepts to be familiar with

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.
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…