The Lambda expression in Java is a new feature that was introduced in Java SE 8. The lambda expressions are basically a block of code that takes a parameter and returns a value with respect to that parameter. The functional interface used by the lambda expression helps to iterate, filter, and extract the data from the collection. There are certain ways through which lambda expressions in Java are implemented.
In this article, we will implement the Java expression by passing different parameters.
How to Implement/Use Lambda Expressions in Java?
The functional interface is the foundation of lambda expressions in Java. The functional interface contains only a single abstract method. The lambda expressions are the implementation of a functional interface. The lambda expressions are quite similar to the methods but are implemented without a name and inside the body. There are certain parameters that can be passed in the lambda expressions according to the need.
Let us advance ourselves and implement lambda expressions in Java.
Example 1: Code in Java Using Lambda Expression
The code below depicts the implementation of a lambda expression with a functional Interface in Java.
//The interface used by the lambda expression
interface drawcircle{
public void draw();
}
public class lambdaexpression{
public static void main(String[] args) {
int circumference=15;
//The lambda expression
drawcircle d=()->{
System.out.println("The Circumference of the Circle is "+circumference);
};
//The method called as declared above
d.draw();
}
}
In the above Java code:
- The code starts with the declaration of the interface as “drawcircle”.
- The public method draw() is invoked for use in the future.
- The next step involves the declaration of circumference as an integer value in the Main class.
- The lambda expression for the “drawcircle” interface is declared.
- The draw() method prints the result.
Output
The output below depicts that the integer value that is 15 is printed using the lambda expression of Java.

Example 2: No Parameters
The code depicts the scenario when no parameters are passed while implementing lambda expressions of Java.
interface lambdaexp {
public String output();
}
//the class for a lambda expression
public class expressionforlambda{
//The Main class of Java
public static void main(String[] args) {
//The lambda expression of Java
lambdaexp s = () -> {
return "The example without any parameter";
};
//The print statement to output the result
System.out.println(s.output());
}
}
In the above code block, a lambda expression doesn’t take any arguments and hence provides the output accordingly.
Output
The below output shows the statement since no arguments were passed in the interface.

Example 3: Single Parameter
The code below describes the implementation of lambda expressions using a single parameter.
interface Singleparameter{
public String sa(String str);
}
public class lambdaexp{
public static void main(String[] args) {
// Single parameter declared in Lambda expression
Singleparameter s1 = (str) -> { return "Hello "+str;
};
System.out.println(s1.sa("Java"));
//Second String
Singleparameter s2 = str ->{ return "Hello "+str;
};
System.out.println(s2.sa("Everyone"));
//Third string
Singleparameter s3 = str ->"Hello "+str;
System.out.println(s3.sa("To programming"));
}
}
In the above code:
- An interface is declared as a “Singleparameter”.
- The Main class of Java consists of lambda expressions for strings s1, s2, and s3.
- The output is printed using the println() method in concatenated form.
Output
In the output below the statements are printed in concatenated form with the string “Hello”.

Example 3: Multiple Parameters
In the code example below there are multiple parameters used to define lambda expressions in Java.
interface integers{
//The integers to be multiplied are declared
int mul(int x, int y); }
public class lambdaexpression{
public static void main(String[] args) {
// More than one parameter in Lambda expression
integers p = (x,y) -> (x*y);
System.out.println(p.mul(10,2));
// More than one parameter in Lambda expression
integers p1 = (int x, int y) -> (x*y);
System.out.println(p1.mul(5,300));
}
}
In this code:
- The Interface is declared as “integers”.
- The x and y are two parameters declared as integers/numbers to be multiplied.
- The lambda expression is utilized to compute the multiplication of given integers.
- The println() method prints the multiplied result respectively.
Output
The output below shows that the lambda expression successfully calculates the product of given integers, i.e., 20 and 1500 respectively.

Conclusion
The lambda expressions in Java consist of a code block that takes the input and returns the value as per the input. These expressions are implemented using the functional interface. In this article, we have described in detail how to use lambda expressions and what types of parameters are used according to the needs of the program.