LAMBDA EXPRESSIONS IN JAVA

INTRODUCTION

java-logoA Lambda Expression we can define to a first approximation as an anonymous method. This immediately tells us two things. The first is that a lambda expression is a method; the second is that the method has no name. To be more precise, however, we must say that a lambda expression is an anonymous class that implements a particular type of interface called a Functional Interface.

FUNCTIONAL INTERFACES

They are the foundation on which Lambda Expression is based. A Functional Interface contains only one abstract method. Let’s look at an example.

Interfaccia funzionale

We could have included a default method as well, but in fact it would not have changed things IMyInterface contains only one abstract method so it is a functional interface.

Default Method

LAMBDA EXPRESSIONS

Let us look at the figure.

Lambda

Let us analyze Lambda Expression. -> is an operator, and it is called a lambda operator. In the left part enclosed in parentheses we define the parameters of the Lambda Expression. In this case there are none however parentheses are mandatory as in the definition of a method. To the right of the lambda operator we have the body, in this case represented by literal 15. If the body consists of only one line, curly brackets do not accrue; if the code is enclosed over several lines, curly brackets must be put in. The return value is missing, which in this case is an int.

Valore di ritorno

Let’s look at another expression.

Lambda

This time (see previous figure) the expression has a parameter, the type is not needed is inferred by the compiler. The expression returns a boolean. When a Lambda Expression has only one parameter the round brackets can be omitted.

n -> n > 0

TARGET TYPE

We will see the relationship that exists between a lambda expression and a functional interface. The functional interface to which we attach the lambda expression is called the Target Type. Let’s look at an example.

Lambda

Lambda expression becomes the code for the method check. We see the whole structure with a figure.

Struttura

We started with the Functional Interface, which has only one abstract method. Then behind the scenes a class is generated that implements this interface and the method code is the lambda expression. Finally, again behind the scenes an instance of this class is produced which we then use. Basically, a Lambda Expression transforms a set of instructions into an object. Let us return to our example, which I quote for convenience.

Lambda

The variable mi is the object we have been talking about, if we now try to execute the method mi.controlla(10) Lambda Expression is executed and the result will be true. The lambda expression must be compatible with the interface method, otherwise the compiler will report an error.

WE USE A LAMBDA EXPRESSION

We will begin to understand what we need lambda expressions for.

Lambda

(n) -> (n % 2) == 0

Checks whether a number is even (remainder of zero division). We see a slightly different form.

Lambda

LAMBDA EXPRESSION WITH A BLOCK OF CODE

A lambda expression consisting of only one instruction is called an Expression Lambda. If it consists of multiple instructions, it is called Block Lambda. Let’s look at an example right away.

Block Lambda

LAMBDA EXPRESSION AS AN ARGUMENT TO A METHOD

It is one of the most widely used in lambda expressions. See the example, the omitted interface code is the one above. As the second parameter of myMethod in the main we go to write a lambda expression compatible with the interface method. We pass definition and code block as method argument, assigned to the variable mi. Let’s look at the test class.

Classe di test

LINKS TO PREVIOUS POSTS

THE JAVA LANGUAGE

LINK TO CODE ON GITHUB

GITHUB

EXECUTION OF THE EXAMPLE CODE

  • Download the code from GITHUB, launch the JAR file with the following command in Visual Studio Code, locating in the directory containing the JAR.

        java -jar –enable-preview CorsoJava.jar

  • Or run the main found in the file CorsoJava.java.