Java Lambda ExpressionsLambda expression is a new and important feature of Java which was included in Java SE 8. It provides a clear and concise way to represent one method interface using an expression. It is very useful in collection library. It helps to iterate, filter and extract data from collection. The Lambda expression is used to provide the implementation of an interface which has functional interface. It saves a lot of code. In case of lambda expression, we don't need to define the method again for providing the implementation. Here, we just write the implementation code. Java lambda expression is treated as a function, so compiler does not create .class file. Functional InterfaceLambda expression provides implementation of functional interface. An interface which has only one abstract method is called functional interface. Java provides an anotation @FunctionalInterface, which is used to declare an interface as functional interface. Why use Lambda Expression
Java Lambda Expression SyntaxJava lambda expression is consisted of three components. 1) Argument-list: It can be empty or non-empty as well. 2) Arrow-token: It is used to link arguments-list and body of expression. 3) Body: It contains expressions and statements for lambda expression. No Parameter Syntax One Parameter Syntax Two Parameter Syntax Let's see a scenario where we are not implementing Java lambda expression. Here, we are implementing an interface without using lambda expression. Without Lambda ExpressionTest it NowOutput: Drawing 10 Java Lambda Expression ExampleNow, we are going to implement the above example with the help of Java lambda expression. Test it NowOutput: Drawing 10 A lambda expression can have zero or any number of arguments. Let's see the examples: Java Lambda Expression Example: No ParameterTest it NowOutput: I have nothing to say. Java Lambda Expression Example: Single ParameterTest it NowOutput: Hello, Sonoo Hello, Sonoo Java Lambda Expression Example: Multiple ParametersTest it NowOutput: 30 300 Java Lambda Expression Example: with or without return keywordIn Java lambda expression, if there is only one statement, you may or may not use return keyword. You must use return keyword when lambda expression contains multiple statements. Test it NowOutput: 30 300 Java Lambda Expression Example: Foreach LoopTest it NowOutput: ankit mayank irfan jai Java Lambda Expression Example: Multiple StatementsTest it NowOutput: I would like to say, time is precious. Java Lambda Expression Example: Creating ThreadYou can use lambda expression to run thread. In the following example, we are implementing run method by using lambda expression. Test it NowOutput: Thread1 is running... Thread2 is running... Java lambda expression can be used in the collection framework. It provides efficient and concise way to iterate, filter and fetch data. Following are some lambda and collection examples provided. Java Lambda Expression Example: ComparatorTest it NowOutput: Sorting on the basis of name... 2 Dell Mouse 150.0 1 HP Laptop 25000.0 3 Keyboard 300.0 Java Lambda Expression Example: Filter Collection DataTest it NowOutput: Iphone 6S: 65000.0 Sony Xperia: 25000.0 Redmi4 : 26000.0 Java Lambda Expression Example: Event ListenerOutput: Next TopicJava 8 Method References |