Java Lambda Expressions
Lambda 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 Interface
Lambda 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
- To provide the implementation of Functional interface.
- Less coding.
Java Lambda Expression Syntax
Java 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 Expression
Output:
Drawing 10
Java Lambda Expression Example
Now, we are going to implement the above example with the help of Java lambda expression.
Output:
Drawing 10
A lambda expression can have zero or any number of arguments. Let’s see the examples:
Java Lambda Expression Example: No Parameter
Output:
I have nothing to say.
Java Lambda Expression Example: Single Parameter
Output:
Hello, tutor Hello, tutor
Java Lambda Expression Example: Multiple Parameters
Output:
30 300
Java Lambda Expression Example: with or without return keyword
In 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.
Output:
30 300
Java Lambda Expression Example: Foreach Loop
Output:
ankit mayank irfan jai
Java Lambda Expression Example: Multiple Statements
Output:
I would like to say, time is precious.
Java Lambda Expression Example: Creating Thread
You can use lambda expression to run thread. In the following example, we are implementing run method by using lambda expression.
Output:
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: Comparator
Output:
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 Data
Output:
Iphone 6S: 65000.0 Sony Xperia: 25000.0 Redmi4 : 26000.0
Java Lambda Expression Example: Event Listener
Output: