lambda expressions in Java
In this post, you will learn about the lambda expressions in the Java programming language.
A lambda function is a function that does not need to declare, but we can pass it immediately as an expression. This is an anonymous function which is usually created during a function call to act as a function parameter. It is similar to method, but they do not need a name. Lambda Expressions were added in Java 8. Java Lambda Expressions are often used in functional programming. These expressions are commonly used to implement simple event listeners/callbacks, or in functional programming with the Java Streams API.
Syntax of Lambda Expression in Java
The given syntax contain single parameter and an expression.
parameter -> expression
We can use more than one parameter by wrapping them in parentheses.
(parameter1, parameter2) -> expression
The parameters can be any numbers, it can be empty or non-empty as well, but the expression should be only one.
Java Lambda Expression With No Parameter
Syntax with no parameter() -> {
// lambda body of no parameter
}
Example-
interface Invitation{
public String msg();
}
public class LambdaExpression{
public static void main(String[] args) {
Invitation x=()->{
return "Welcome to etutorialspoint!";
};
System.out.println(x.msg());
}
}
Output of the above code-
Welcome to etutorialspoint!
Java Lambda Expression With One Parameter
Syntax with one parameter(p1) -> {
//Body of single parameter lambda
}
Example-
interface Invitation{
public String msg(String name);
}
public class LambdaExpression{
public static void main(String[] args) {
Invitation x=(name)->{
return "Welcome to "+name;
};
System.out.println(x.msg("etutorialspoint!"));
}
}
Output of the above code:
Welcome to etutorialspoint!
Java Lambda Expression With Multiple Parameters
Syntax with no parameter(p1,p2) -> {
//Body of multiple parameter lambda
}
Example-
interface Addition{
int add(int a,int b);
}
public class LambdaExpression{
public static void main(String[] args) {
Addition x=(a,b)->(a+b);
System.out.println(x.add(10,20));
}
}
Output of the above code:
30
Uses of Java Lambda Expression in Traversal
In the given Java program, we have used the Lambda expression in the iteration to print the every item in the list.
import java.util.*;
public class LambdaExpression{
public static void main(String[] args) {
ArrayList<String> arrList=new ArrayList<String>();
arrList.add("Smith");
arrList.add("Aditya");
arrList.add("John");
arrList.add("Paul");
arrList.add("Nail");
arrList.forEach(
(n)->System.out.println(n)
);
}
}
Output of the above code:
Smith
Aditya
John
Paul
Nail
Related Articles
Number pattern programs in JavaJava program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number