Agenda:
- Understanding Lambda Expressions and its syntax.
- 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 totestDrive
method and usingneedToTest
method ofCarTester
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.
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
Please take my Java Course for video lectures.This article is part of the series of articles to learn Java programming language from Tech Lead Academy:Introduction to programming
OS, File, and File System
Working with terminal
Welcome to Java Programming Language
Variables and Primitives in Java
Convert String to numeric data type
Input from the terminal in Java
Methods with Java
Java Math Operators and special operators
Conditional branching in Java
Switch statement in Java
Ternary operator in Java
Enum in Java
String class and its methods in Java
Loops in Java
Access modifiers in Java
Static keyword in Java
The final keyword in Java
Class and Object in Java
Object-Oriented Programming in Java
OOP: Encapsulation in Java
OOP: Inheritance in Java
OOP: Abstraction in Java
OOP: Polymorphism in Java
The method Overriding vs Overloading in Java
Array in Java
Data Structures with Java
Collection framework in Java
ArrayList in Java
Set in Java
Map in Java
Date and Time in Java
Exception in Java
How to work with files in Java
Design Patterns
Generics in Java
Multithreading in java
Annotations in Java
Reflection in Java
Reflection & Annotations - The Powerful Combination
Run terminal commands from Java
Lambda in Java
Unit Testing in Java
Big O Notation for coding interviews
Top Java coding interview questions for SDET