Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Functional Interface and Lambda Expression

DZone's Guide to

Functional Interface and Lambda Expression

Check out this tutorial to learn more about using the functional interface and lambda expression in Java.

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

Welcome to this tutorial on the functional interface and lambda expression. In this tutorial, we will discuss the important concepts of the functional interface and lambda expression. Let's get into it!

Functional Interface:

The functional interface is an interface with one abstract method, but it can have any number of default and static methods. The runnable and comparable interface are examples of a functional interface.

Example:

public interface TestInterface {
public abstract void show(int n);
public static void hello() {
System.out.println("static method");
}
public default void test() {
System.out.println("default method");
}
}

 @FunctionalInterface Annotation:

We use the @Functional annotation with a functional interface. This ensures that the interface can’t have more than one abstract method. If we define more than one abstract method in this interface, then the compiler shows an error ‘Invalid '@FunctionalInterface' annotation.

@FunctionalInterface
public interface TestInterface {
public abstract void show(int n);
public static void hello() {
System.out.println("static method");
}
public default void test() {
System.out.println("default method");
}
}

Lambda Expression:

The lambda expression is an anonymous method (a method without a name) that is used to implement the abstract method of the functional interface.

The lambda expression introduces a new operator in Java called the ARROW ( -> )   operator. It divides the lambda expression into two parts:

(n) - > System.out.println(n);


In regards to the left-side, specify-required parameter of the implemented method, it can be empty if no parameter is required. As far as the right-side of the lambda expression, you will need to specify the action of the lambda expression (body of the implemented method);

In Java 7, when we want to use functional interface in our class, we have to implement the interface and either override the abstract method or use the anonymous class of that interface.

Example 1:  Let’s Create a Thread Without the Lambda Expression.

public class LambdaTest implements Runnable{
  public void run() {
    System.out.println("thread running");
  }    
 public static void main( String[] args ){
    Thread t = new Thread(new LambdaTest()); 
    t.start();    
   }
}


But, in Java 8, it is not necessary to implement the functional interface in our class. We can use it with a lambda expression, as shown below.

Example 2: Thread Creation With the Lambda Expression

public class LambdaTest{
public static void main( String[] args ){
Runnable run = () -> { 
System.out.println("Thread");
};
/* you can also write like this

Runnable run = () -> System.out.println("Thread");
*/
 Thread t = new Thread(run);
 t.start();
 }
}


From the above example, we have noticed that the thread creation with the lambda expression results in simpler, clean code.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
functional interface ,java ,java8 ,lambda expression ,thread ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}