
Lambda expressions aided the developer in simplifying the development process. The lambda expression is important feature of Java 8. This functionality is highly useful in collections for iterating and filtering the data.
It does not have a return type or even a method name, and it does not specify accessibility. Classes and objects are given less weight in lambda expressions. It’s sometimes referred to as anonymous methods.
The Function interface is implemented by Lambda Expression, which saves a lot of code throughout the development process. All you have to do now is write the implementation code.
Table of Contents
Java Lambda Expression example
We’ll show you how to use a method that just displays one message. We’ll look at two examples in this section. One technique is to create a simple java method, while the other is to use a lambda expression to create the equivalent method.
public void show(){ System.out.println("Hello, Geeks!"); }
Equivalent Lambda Expression
()->{System.out.println("Hello, Geeks!");}
What is Functional Interface with example?
The idea of a functional interface, which contains just one abstract method, was introduced in Java 8.
If someone asks what a functional interface is? You might respond that it’s an interface with only one abstract method.
We need to use the annotation @FunctionalInterface to indicate that this is a functional interface.
@FunctionalInterface public interface FunctionInterfaceDemo { void performAction(); }
It will throw an exception if you try to write more than one method in a functional interface (if that interface is specified with @FunctionalInterface annotation).
Example of Functional Interface in Java 8
To use a lambda expression, we should first design a functional interface. A method can also take another functional interface reference as a parameter.
interface FunctionalInterfaceDemo{ void performCalculation(double h, double w); } public class Rectangle { public static void main(String[] args) { FunctionalInterfaceDemo fi=(h, w)->{ System.out.println("Area of Rectangle is "+ h * w); }; fi.performCalculation(10, 20); } }
Output: Area of Rectangle is 200.0
In Java, how do you write a lambda expression?
The following are some examples of how to construct lambda expressions in Java.
The number of arguments in a expression might be zero, one, or more.
()->System.out.println("Hello Geeks, Lambda Expression");
It is not necessary for the programmer to specify the type of argument. If the compiler can determine the type of the argument,
(x, y)->System.out.println("Addition = "+(x + y));
We have to use commas to separate parameters.
(double x, double y) or (x, y)
When there is a no parameter we must use empty parenthesis.
()->System.out.println("Hello, World!");
We don’t need to use parentheses if the type of a single argument is known to the compiler.
(int n) -> return n;
n -> return n + 10;
We should encapsulate them in curly brackets if they have more than one statement in their body. When there is only one statement, curly braces are not required.
()->{ System.out.println("Statement 1"); System.out.println("Statement 2"); }
()->System.out.println("Only one Statement");
Creating a Thread Using Lambda Expressions
This is an example of a lambda expression used to run a thread.
In this example we have created two threads using Runnable interface. First thread is running without using expression and second thread is running using lambda expression.
public class LambdaExpressionThread{ public static void main(String[] args) { Runnable runnable1 = new Runnable() { public void run() { System.out.println("This is thread 1"); } }; Thread thread1 = new Thread(runnable1); thread1.start(); // Example of thread using lambda expression Runnable runnable2 = () -> { System.out.println("This is thread 2"); }; Thread thread2 = new Thread(runnable2); thread2.start(); } }
Output: This is thread 1 This is thread 2
Click here to read Java 8 popular interview questions and answers