In Java, Lambda expressions basically express instances of functional interfaces (An interface with a single abstract method is called a functional interface). Lambda Expressions in Java are the same as lambda functions which are the short block of code that accepts input as parameters and returns a resultant value. Lambda Expressions are recently included in Java SE 8.
Functionalities of Lambda Expression in Java
Lambda Expressions implement the only abstract function and therefore implement functional interfaces lambda expressions are added in Java 8 and provide the below functionalities.
- Enable to treat functionality as a method argument, or code as data.
- A function that can be created without belonging to any class.
- A lambda expression can be passed around as if it was an object and executed on demand.
Java Lambda Expression Example
Java
interface FuncInterface
{
void abstractFun( int x);
default void normalFun()
{
System.out.println( "Hello" );
}
}
class Test
{
public static void main(String args[])
{
FuncInterface fobj = ( int x)->System.out.println( 2 *x);
fobj.abstractFun( 5 );
}
}
|
Lambda Expression Syntax
lambda operator -> body
Lambda Expression Parameters
There are three Lambda Expression Parameters are mentioned below:
- Zero Parameter
- Single Parameter
- Multiple Parameters
1. Lambda Expression with Zero parameter
() -> System.out.println("Zero parameter lambda");
2. Lambda Expression with Single parameter
(p) -> System.out.println("One parameter: " + p);
It is not mandatory to use parentheses if the type of that variable can be inferred from the context
Java
import java.util.ArrayList;
class Test {
public static void main(String args[])
{
ArrayList<Integer> arrL = new ArrayList<Integer>();
arrL.add( 1 );
arrL.add( 2 );
arrL.add( 3 );
arrL.add( 4 );
arrL.forEach(n -> System.out.println(n));
arrL.forEach(n -> {
if (n % 2 == 0 )
System.out.println(n);
});
}
}
|
Note: that lambda expressions can only be used to implement functional interfaces. In the above example also, the lambda expression implements Consumer Functional Interface.
3. Lambda Expression with Multiple parameters
(p1, p2) -> System.out.println("Multiple parameters: " + p1 + ", " + p2);
A Java program to demonstrate the working of a lambda expression with two arguments.
Java
public class Test {
interface FuncInter1 {
int operation( int a, int b);
}
interface FuncInter2 {
void sayMessage(String message);
}
private int operate( int a, int b, FuncInter1 fobj)
{
return fobj.operation(a, b);
}
public static void main(String args[])
{
FuncInter1 add = ( int x, int y) -> x + y;
FuncInter1 multiply = ( int x, int y) -> x * y;
Test tobj = new Test();
System.out.println( "Addition is "
+ tobj.operate( 6 , 3 , add));
System.out.println( "Multiplication is "
+ tobj.operate( 6 , 3 , multiply));
FuncInter2 fobj = message
-> System.out.println( "Hello " + message);
fobj.sayMessage( "Geek" );
}
}
|
OutputAddition is 9
Multiplication is 18
Hello Geek
Note: Lambda expressions are just like functions and they accept parameters just like functions.
Conclusion
Some Important points intake from this article is mentioned below:
- The body of a lambda expression can contain zero, one, or more statements.
- When there is a single statement curly brackets are not mandatory and the return type of the anonymous function is the same as that of the body expression.
- When there is more than one statement, then these must be enclosed in curly brackets (a code block) and the return type of the anonymous function is the same as the type of the value returned within the code block, or void if nothing is returned.
FAQs in Lambda Expression
Q1. What type of lambda expression Java?
Answer:
Java Lambda Expressions are the short block of code that accepts input as parameters and returns a resultant value.
Q2. Is it good to use lambda expressions in Java?
Answer:
Yes, using lambda expressions makes it easier to use and support other APIs.
Q3. What are the drawbacks of Java lambda?
Answer:
Java lambda functions can be only used with functional interfaces.
Q4. Based on the syntax rules just shown, which of the following is/are NOT valid lambda expressions?
- () -> {}
- () -> “geeksforgeeks”
- () -> { return “geeksforgeeks”;)
- (Integer i) -> return “geeksforgeeks” + i;
- (String s) -> {“geeksforgeeks”;}
Answer:
4 and 5 are invalid lambdas, the rest are valid. Details:
- This lambda has no parameters and returns void. It’s similar to a method with an empty body: public void run() { }.
- This lambda has no parameters and returns a String as an expression.
- This lambda has no parameters and returns a String (using an explicit return statement, within a block).
- return is a control-flow statement. To make this lambda valid, curly braces are required as follows: (Integer i) -> { return “geeksforgeeks” + i; }.
- “geeks for geeks” is an expression, not a statement. To make this lambda valid, you can remove the curly braces and semicolon as follows: (String s) -> “geeks for geeks”. Or if you prefer, you can use an explicit return statement as follows: (String s) -> { return “geeks for geeks”; }.