Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Lambda Expression & Functional Interface

1. Lambda Expression Introduction

Lambda expressions are new feature of Java 8. With Lambda Expression interface implementation becomes more easier compared with earlier releases of Java. In this example, we will learn about Lambda expression and how to use it for Functional Interface. When an interface contains only one abstract method, we call it as functional interface.

2. The Runnable Interface

Java provides an interface called Runnable which one can use to create threads. Look at the interface definition below:

Fig 1. Runnable Interface with Only One Method
Fig 1. Runnable Interface with Only One Method

Explanation

  1. Shows the Runnable interface declaration.
  2. The interface contains only one contract method called run. This method does not take any arguments and returns nothing.
  3. The interface is marked as Functional Interface via @FunctionalInterface. When an interface contains only one method, we call it as Functional Interface.

In this example, we will use this Functional Interface to explore the Lambda Expression.

3. Anonymous Inner Class

Anonymous Inner class is an inner class with no name. They are useful to define the handler functions or to provide the interface implementation on the fly. Now, look at the below example which implements the run method of the Runnable interface using anonymous inner class technique:

Fig 2. Anonymous Inner Class
Fig 2. Anonymous Inner Class

Explanation

  1. The outer class contains the main method which want to start a thread. The class name is Ex002_AnnInClass.
  2. When we compile the code, we get a class file (Compiled object code) for this outer class.
  3. Thread constructor takes a class which implements the Runnable interface. Instead of creating a new class to implement the Runnable interface, we create anonymous inner class. In the step chart above, after new Runnable() we open a pair of Curley braces and implement the run method. The method is inside the nameless inner class (Anonymous class) which simply prints a message in the console window.
  4. In the compiled output, we can see this nameless inner class as MainClassName$1.

We can convert this Anonymous Inner class as a Lambda Expression. Let us do that.

4. Lambda Expression

In the below code, we create a thread and provide Runnable implementation as Lambda Expression. Note, Runnable interface expects to provide implementation for only one method.

Fig 3. Implement Runnable Interface as Lambda Expression
Fig 3. Implement Runnable Interface as Lambda Expression

Explanation

  1. Shows the thread creation as well as providing the run method override of the Runnable interface.
  2. Java knows that Thread class constructor can take a class which implements the Runnable interface. The parenthesis here states the run method. Note, we do not specify the name ‘run’ here as it is the only method defined in the Runnable interface.
  3. The arrow mark here states the body of the run method starts.
  4. There is only one statement in the method body which is a function call to println. Since println returns void, it qualifies as a Lambda Expression (note run method override won’t take any param and returns nothing).

Running both the examples, starts the thread and prints message in the console window.

5. Code Reference

5.1 Anonymous Inner Class

5.2 Lambda Expression Example

6. Summary

Lambda Expression suits well for the Functional Interfaces. Here, we saw how we used the anonymous inner class to implement thread & how we converted it even more simplified Lambda.

Categories: Java

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.