Lambda In Java

Beknazar
4 min readMar 31, 2022

--

Agenda:

  1. Understanding Lambda Expressions and its syntax.
  2. Built-in functional interfaces.

Understanding Lambda Expressions and its syntax

Starting from version 1.8 Java supports Lambda Expressions. Java is an object-oriented programming language, however, lambda is adding some functional programming features to java.

It is mainly the ability to pass a method as an argument to another method.

Let’s start with an example

In the above snippet, we have a Car class with properties and we have TestDrive class with one testDrive method. The main idea of this method is to call the drive method for the different models of cars. Right now it is testing only Tesla cars. If we need to test different models as well, we need either change this method by passing the argument for a model or create new methods for other models.

We will go with an extra argument

public void testDrive(List<Car> cars, String modelToTest) {        
for (Car car : cars) {
if (car.model.equals(modelToTest)) {
car.drive();
}
}
}

we are good for now with this method, but let’s say now they want to test cars only from the 2018 year. It would be great if we could pass the filter as a method to our method. And that’s exactly what Lambda can do.

To start we need to define the Function Interface. It is an interface with only one abstract method. That interface can be used then as an argument for our testDrive method so we can use Lambda syntax to pass implementation for that abstract method.

  • I defined our CarTester function interface where we provided one abstract method that accepts the Car object and returns a boolean.
  • Added CarTester as an argument to testDrive method and using needToTest method of CarTester to see what car needs to be tested.
  • Using lambda syntax in the main method to define on which car object to run the drive method.
// test Tesla and Audi models
testDrive.testDrive(cars, (Car e) -> {
return e.model.equals("Tesla") || e.model.equals("Audi");
});

here, we are using lambda syntax. The second argument looks like a method but without a name. We refer to these methods as anonymous methods. So basically if we have a functional interface as an argument for our method, we can pass the anonymous method as an implementation of the abstract method of our functional interface(that’s the reason it can have only one abstract method).

Let’s break down the syntax of the anonymous method.

Now with this syntax, I can define which car to test from the client-side code just by providing different anonymous methods.

// test white cars only
testDrive.testDrive(cars, (Car e) -> {
return e.color.equals("white");
});

The lambda syntax can be tricky because there are many optional parts.

// test all white cars
testDrive.testDrive(cars, e -> e.color.equals("white"));

The above snippet is a valid way of passing the anonymous method as well.

Built-in functional interfaces

Java has many built-in functional interfaces so we don’t have to create interfaces for commonly used functions.

common functional interfaces

how do we use them?

Let’s rewrite the above example using Predicate

  • I removed my own interface and just used Predicate
  • The rest of the code is the same and it works as expected.

Let’s see some examples where java utilizes these built-in functional interfaces

--

--