Home  >  Article  >  Java  >  Java Lambda Expressions

Java Lambda Expressions

WBOY
WBOYOriginal
2024-08-30 16:16:201049browse

Lambda Expressions are the new feature addition and introduced in Java8. It basically describes all the instances of functional interfaces. Its implementation works in a way that it includes only one abstract function and implements the functional interfaces. Java 8 Lambda Expression can create a method argument or consider the entire code as data. It can implement a function that does not have any specific class, which means an abstract class. This expression can be passed to the next object and can be executed whenever required. Lambda Expressions can be considered just like functions and can accept parameters like other functions.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

lambda_operator -> body

Parameters of Java Lambda Expressions

Parameters passed to the lambda_operator are as follows:

  • # Zero Parameter: This Parameter means it cannot pass any of the arguments from the function, which is represented as below:
  • (): System.out.println(“Zerolambda argument”);
  • # One Parameter: This Parameter means it can pass any one of the arguments from the function, which is represented as below:
  • (p): System.out.println(“One argument: ” + p);
  • # Multiple Parameter: As the name suggests, this parameter can be used to pass more than one parameter to the operator, and it is represented as below:
  • (q1, q2): System.out.println(“Multiple arguments: ” + q1 + “, ” + q2);

Why do we need Java Lambda Expressions?

Lambda Expressions came into existence with Java 8 and has transformed and simplified all the complex execution of the block of code that can be passed around for execution. It is a very common feature that many other programming languages use, and so does Java 8; after its introduction, the earlier feature of java first needs the creation of an object and then pass the object around, such as for making a strategized design pattern. But lambda expressions came into the savior of the above-described problem as it is a value addition for enhancing the functional interface implementation. It has simplified the implementation with the lambda_operator function pointing towards the body of the program possessing zero, one, and multiple parameters to be passed on to the function. It also makes use of the function which gets created and not belonging to any defined classes. It has a very good feature that can adopt the functionality as a method argument of the function, which is considered data as a whole.

Lambda Expressions in Java 8 is very powerful and very compelling. It is highly recommended to convert the objects related to the functional interface for its final output.

How do Java Lambda Expressions work?

There is a hidden working pattern in any of the expressions, so as Lambda Expression it also posses some working pattern which is represented as follows:

(int arg_a, String arg_b)
{System.out.println("two arguments"+ arg_a+" and "+arg_b);}

These Two arguments int arg_a and String arg_b constitute the list of arguments with zero arguments, one argument, or more than two-argument, i.e. multiple arguments. These arguments as an argument list are passed over to the lambda expression’s body with the arrow token’s help. This arrow token with its argument list continuously follows the lambda body. Also, lambda expression in this format further makes use of a functional interface for its implementation. But if it is multiple argument lists, then it is mandatory to close the brackets or the block of code and then that return type of the anonymous function will be the same as the type of value which needs to return the code of block or void if that is not returned or possessing any value for it.

Examples to Implement of Java Lambda Expressions

Below are the examples of Java Lambda Expressions:

Example #1

This program illustrates the interface execution without lambda expressions being used and not even by creating an abstract class for printing the size of the rectangle.

Code:

interface Shapes
{
public void rectangle();
}
public class WithoutLambdaExpression {
public static void main(String[] args) {
int size=15;
Shapes wd=new Shapes(){
public void rectangle()
{
System.out.println("Get the shape" +  size );
}
};
wd.rectangle();
}
}

Output:

Java Lambda Expressions

Example #2

This program illustrates the functional interface creation using the lambda Expressions, thereby providing the shapes’ required output.

Code:

interface Shapes{
public void Rectangle();
}
public class UsinglmbdaExpression {
public static void main(String[] args) {
int size=20;
Shapes r8=()->
{
System.out.println("Shapes of all sizes "+ size);
};
r8.Rectangle();
}
}

Output:

Java Lambda Expressions

Example #3

This program is used to illustrate the Lambda Expression for Java 8 by not passing any parameter implemented just with the function parameter and the associated abstract classes of the implemented functional interface as shown.

Code:

interface SomethingFishy{
public String strange();
}
public class LambdaWithNoArg{
public static void main(String[] args) {
SomethingFishy k=()->{
return "Very Strange View.";
};
System.out.println(k.strange());
}
}

Output:

Java Lambda Expressions

Example #4

This program illustrates the lambda Expression with the implementation of functional parameters, thereby passing the single parameter from the function of the interface and the lambda expression associated with it.

Code:

interface JuicyFruit{
public String juicy(String name);
}
public class SinglParamLambda
{
public static void main(String[] args)
{
JuicyFruit jf1=(name)->{
return "Kiwi, "+name;
};
System.out.println(jf1.juicy("orange"));
JuicyFruit jf2= name ->{
return "Mango, "+name;
};
System.out.println(jf2.juicy("Pineapple"));
}
}

Output:

Java Lambda Expressions

Example #5

This program is used to illustrate the Multiple parameter lambda Expression with the functional interface of division and its values to be divided using the class’s div function.

Code:

interface Division{
int div(int p,int q);
}
public class MultiParamLambdaExpression{
public static void main(String[] args) {
Division div1=(p,q)->(p/q);
System.out.println(div1.div(20,40));
Division div2=(int p,int q)->(p/q);
System.out.println(div2.div(400,600));
}
}

Output

Java Lambda Expressions

Note: Lambda expressions are exclusively used to implement the functional interfaces, and they can contain any type of argument such as zero, two or multiple.

Conclusion

Lambda Expression introduced with Java 8 has simplified the execution of the block of code using the array operator and operator arrow pointing to the lambda body, and then it will be used for implementing the interfaces with the abstract classes associated with each class. Thus, Lambda Expression of Java 8 has really transformed the interface interaction and the implementation of the functions and method easier with the abstract classes.

The above is the detailed content of Java Lambda Expressions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Java ServerSocketNext article:Java ServerSocket