How to Start Working With Lambda Expressions in Java

How to Start Working With Lambda Expressions in JavaFoundation concepts to be familiar withLuis SantiagoBlockedUnblockFollowFollowingDec 23, 2017Photo by Goran Ivos on UnsplashBefore 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 introductionLambda 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 interfaceA 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 InterfaceThe arrow operatorLambda expressions introduce the new arrow operator -> into Java.

It divides the lambda expressions in two parts:(n) -> n*nThe 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 TestGreeting LambdaThe 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 TypeBlock lambda expressionsSo 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 LambdaGeneric functional interfacesA 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 InterfaceLambda expressions as argumentsOne 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 ArgumentThese 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.


. More details

Leave a Reply