
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Concurrent Programming Approach Using Lambda Expressions
An important addition to Java SE 8 is the lambda expressions feature. A clear and concise expression of method interfaces is possible using expressions. The collection library is extremely helpful. Collections can be iterated, filtered and have data extracted for useful purposes. To implement functional interfaces, lambda expressions are used widely. A lot of code is saved by it. Lambdas expressions allow us to provide the implementation without having to redefine the method. Only the implementation code takes shape in this place through writing. The compiler does not create a. Because Java lambda expressions are considered functions .class file.
Functional Interface
@FunctionalInterface is a Java annotation that declares an interface as functional. A functional interface is an interface that only has one abstract method. Lambda expression lets you implement this functional interface.
Why use Lambda Expression?
It provides the implementation of functional interface.
It offers less coding.
Java Lambda Expression Syntax
(argument-list) { //body }
It consisted of three components ?
Argument-List ? Can be empty or non-empty as well
Arrow-Taken ? Used to link the Argument-list and body for the expression
Body ? Contains expressions and statements for lambda expression
No Parameter Syntax
() { // body of no parameter lambda }
One Parameter Syntax
(p1) { // body of single parameter lambda }
Two Parameter Syntax
(p1,p2) { //body of multiple parameter lambda }
Example: Java Lambda Expression
@FunctionalInterface //It is optional interface Drawable{ public void draw(); } public class LambdaExpressionExample2 { public static void main(String[] args) { int width=10; //with lambda Drawable d2=()->{ System.out.println("Drawing "+width); }; d2.draw(); } }
Output
Drawing 10
Example: Without Lambda Expression
interface Drawable{ public void draw(); } public class LambdaExpressionExample { public static void main(String[] args) { int width=10; //without lambda, Drawable implementation using anonymous class Drawable d=new Drawable(){ public void draw(){System.out.println("Drawing "+width);} }; d.draw(); } }
Output
Drawing 10
Example: No Parameter
interface Sayable{ public String say(); } public class LambdaExpressionExample3{ public static void main(String[] args) { Sayable s=()->{ return "Don't settle for average."; }; System.out.println(s.say()); } }
Output
Don't settle for average
Example: Single Parameter
interface Sayable { public String say(String name); } public class LambdaExpressionExample4{ public static void main(String[] args) { // Lambda expression with single parameter. Sayable s1=(name)->{ return "Hello, "+name; }; System.out.println(s1.say("World")); // You can omit function parentheses Sayable s2= name ->{ return "Hello, "+name; }; System.out.println(s2.say("World")); } }
Output
Hello, World Hello, World
Example: Multiple Parameter
interface Addable{ int add(int a,int b); } public class LambdaExpressionExample5{ public static void main(String[] args) { // Multiple parameters in lambda expression Addable ad1=(a,b)->(a+b); System.out.println(ad1.add(20,20)); // Multiple parameters with data type in lambda expression Addable ad2=(int a,int b)->(a+b); System.out.println(ad2.add(200,200)); } }
Output
40 400
Conclusion
Using expressions, Java SE 8 offers a valuable addition through the lambda expressions feature. The ability to express method interfaces in a clear and concise manner has become effortlessly possible. Iterating, filtering, and extracting data are among the many practical uses provided by the collection library. Implementing functional interfaces has seen widespread usage of lambda expressions, and it results in considerable code reduction. An excellent feature of the lambda expression is the ability to give the implementation without redundant method redefinition. In this space, writing gives implementation code its form. There is no creation by the compiler, as a .class file is not formed. As functions are viewed as Java lambda expressions.