Home >Java >javaTutorial >Java Lambda Expressions
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 passed to the lambda_operator are as follows:
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.
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.
Below are the examples of Java Lambda Expressions:
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:
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:
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:
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:
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
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.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!