Bits of Java – Episode 21: Lambdas Expressions and Functional Interfaces

This week we are going to discuss about lambdas expressions in Java. They are a way to write code in a more functional way, and they could be quite useful when you need to perform just a simple operation, and you do not want to write a dedicated method just for that.

One of the simplest lambda expression you can write is:

In here you are just saying something like "Giving the parameter s, performs the action System.out.println(s)". So, what comes before the arrow are the lambda parameters, and what comes after is the lambda body.

There are a few syntax rules to keep in mind when working with lambdas:

  • If you specify the type of the parameter, or if you have multiple parameters, then you need to enclose them within brackets:

  • If you specify the type for one of the parameters, then you need to do the same for the other parameters as well:

  • If the lambda body requires more than one statement, then you have to enclose it within curly brackets and use the return statement:

  • If you use var instead of the type for a lambda parameter, then you have to do the same for the other parameters as well:

There are also some limitations on the use of variables within a lambda body. In particular, while class and instance variables can be called from within a lambda body without any issue, local variables can be called only if they are effectively final. This means that, or they are marked with the final specifier, or they act as if they were, in the sense that their value is never changed after initialization.

The type of the pred and cons variable used in the previous example are two of the Java built-in functional interfaces. What are these things exactly? They are interfaces which define one and only one abstract method. They can have also other members defined, but one and only one abstract method!

So, for instance, Predicate is defined as:

So, what you are doing when defining a Predicate through a lambda expression is simply providing an implementation for such an abstract method, in a short way. The parameter of the lambda expression is evaluated at runtime and we can do something like:

Consumer is another built-in Java functional interface, and its abstract method takes one parameter of generic type T and returns nothing.

There are other useful built-in functional interfaces in Java. Among them, we could mention Supplier, which takes no input parameters, but returns an object of type T.

Another one is Comparator. This takes two input parameters and returns an int, which represents the result of the comparison between the two parameters.

To compare two objects, the convention is to return a positive number whether the first object is larger, a negative number in the other case, and zero whether the two objects are the same based on the comparison criterion.

Then, of course, you can also make your own functional interfaces. If you are not sure whether an interface you have defined can be used as functional interface, just try to add the annotation @FunctionalInterface to it. If the compiler complains, then you are probably not working with a valid functional interface.

And that's it for today!

Next week we will discuss some of the differences between Comparator, that we saw today, and Comparable!

By Ilenia Salvadori