Lambda Expressions in Java 8 (Part 1)

Kasun Dissanayake
4 min readApr 17, 2019

Lambda is a new programming construct which lets you build Java Applications in a completely different paradigm. This tutorial will cover Java 8 Lambda and you need previous knowledge on Java programming and coding experience on previous versions of Java. It may be Java 6 or Java 7.

I have used NetBeans IDE for the demonstration .

Here we are focusing only on Java 8 in this tutorial and specifically Lambda functions in Java 8.

Here is the overview of this tutorial series.

  • Understanding Lambdas.
  • Using Lambdas.
  • Functional Interfaces.
  • Method references.
  • Collections Improvements .

Why Lambdas?

Most of the developers use lambda expressions for implementation because of these things,

  • Enables functional programming
  • Readable and concise code
  • Easier-to-use APIs and libraries
  • Enables support for parallel processing

Functional Programming and Object Oriented Programming

We have used Object Oriented Programming for many many years. Is that good enough? Why do we use Functional Programming? In Object Oriented Programming we are using millions of code lines. You can do pretty much anything in Java using just Object Oriented Programming. So Functional Programming let you write more readable and maintainable code. Functional Programming is another programming paradigm which let you write code in certain situations. This is like another tool in your toolbox to use when in the certain kind of situations.

In OOP pretty much everything is an Object. You can not have like a piece of logic or functionality that exists in isolation. It has to be a part of a Class or an Object. All code blocks are associated with classes and objects.

Let me show this Example.

If I execute this I should get Hello World print in the console.

The print method prints Hello World. Now I want it to take input and do different things based on the input. This print method accept an argument and that argument lets the print method know what to do. One way to do this is to write the print method with all possible combinations of all it can do and have the input argument as a switch. But it is not a good thing. You can pass a behavior as an argument and the print method just take that behavior and it performs it. One way to do this in Java 7 is by creating a Class or an Interface called Print which consists a perform method. And you pass to the print method an instance of this Print Interface.

Print Interface here.

package lambdatutorial;/**
*
* @author Kasun Dissanayake
*/
public interface Print {
public void perform();

}

An implementation of Print Interface here. It just print welcome to the console.

package lambdatutorial;/**
*
* @author Kasun Dissanayake
*/
public class PrintPerform implements Print {
@Override
public void perform() {
System.out.println("Welcome...");
}

}

The main class here. You just want to create an instance of PrintPerform class and pass it to print method on LambdaTutorial class.

package lambdatutorial;/**
*
* @author Kasun Dissanayake
*/
public class LambdaTutorial {
public void print(Print print){
print.perform();
}
public static void main(String[] args) {

LambdaTutorial lambdaTutorial = new LambdaTutorial();
PrintPerform printperform = new PrintPerform();
lambdaTutorial.print(printperform);


}

}

Now run the main class. You will get this output.

Here I am passing the behavior to the print method. I can easily create new implementation of the Print interface and change the code easily. This is classic Object Oriented Programming.

But here we had to do some extra work. You are not just passing a behavior. We are passing a thing that has a behavior (Here we are passing Print which has a method perform) . But we need to pass just an action. And execute the action.

Lambda setup to achieve these things. Lambda lets you create these entities which are just functions. They are called Lambda Expressions. They do not belong to a class. They are just functions which exist in isolation. We will learn about Lambda expressions more in next tutorial.

Thank You!

--

--

Kasun Dissanayake

Senior Software Engineer at IFS R & D International || Former Software Engineer at Pearson Lanka || Former Associate Software Engineer at hSenid Mobile