Lambdas in Java

Lambdas in Java can be a little complex to understand at first, but I promise you, they aren’t! In fact, lambdas allow us to write our programs faster, with fewer bugs along with ending up with cleaner code. They also allow for functional programming, however this article will not be diving in to the ins and outs of functional programming in Java – only a basic overview of what lambdas are and how we write them.

Before Lambdas…

Before we even start talking about what lambdas are, let’s take a step back and look at the basic structure of a method.

This is pretty basic right? Well, try to think of what a method really is in Java. A block of code – steps that are executed sequentially. On top of this, we write this method in a class. The methods we write in that class are actions which that class can perform. The methods are a part of the class. However, what if we wanted to pass this behavior around? Before Java 8, we would do something like the following:
This is basic OOP in a nutshell. If we wanted to create a new behavior (implementation), all we would need to do is create a new class which implements the  PrintText interface, create a new instance of it and pass it to our  printSomething() method. Still, what we’re passing in to printSomething()  is an object which implements our behavior. Now, what if wanted this behavior in isolation? Without having to create a new class implementing our interface? What if we were able to assign this code to a variable, in the same way we define other objects or primitives?

Introducing Lambdas

What we want to achieve with lambdas is something like the following:

Note: the syntax for this section is for example purposes

What we want to do is assign this method to reference so we can easily pass it around, in isolation from a class. How do we do this? When converting a method to a lambda, we can get rid of a few things. First of all, we can remove the access modifier, things like public , private , or protected . We can remove the method name as well since what we want to do is refer to a function, not to a reference of a function. We can also remove the return type as the Java compiler is smart enough to infer it based on the statement. What we’re left with then, is something like this:

The final piece we need to realize our lambda expression is the ->  symbol. We place it after our parameters. In this case, we put them like so:
We can even take this a step further and remove the curly braces.
However, this is allowed only if our statement is one line. For anything more, you will need to use the traditional curly braces.

Lambdas In Practice

Now that we know how to write lambdas, how do we go about writing our own? Let’s try to solve our original problem of passing around the printToConsole()  method in isolation from a class which implements the PrintText interface. Creating lambdas is very simple. All we need to do is create an interface and define, only, one method which will be the signature for our lambda. Our original PrintText interface does just this. Instead of passing our Printer implementation, we can pass a lambda.

You may notice that we define a type for our lambda, this is necessary for creating references. However, we can also inline our lambda, so we do not even need to have a reference to it, like so:

Returning Type and Parameters

Creating lambdas with return types and parameters is just as easy as creating ones that are void . Let’s say we want to create a lambda that multiplies two integers and returns the product. We would create the following functional interface:

…with the following code showing how we would use it:

We can also pass this reference as we did in the previous example like so:

Since we defined a return type in our functional interface, we can see that the compiler knows to return an int. Also, for functions taking parameters, we pass the behavior, not the function along with its arguments.

 

Further Reading

Oracle docs – Lambdas

 

Liked the article? Share it!

Leave a Reply

Be the First to Comment!

avatar
  Subscribe  
Notify of