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.
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
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 ExpressionTest it Now
Java Lambda Expression Example
Now, we are going to implement the above example with the help of Java lambda expression.Test it Now
A lambda expression can have zero or any number of arguments. Let's see the examples:
Java Lambda Expression Example: No ParameterTest it Now
I have nothing to say.
Java Lambda Expression Example: Single ParameterTest it Now
Hello, Sonoo Hello, Sonoo
Java Lambda Expression Example: Multiple ParametersTest it Now
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.Test it Now
Java Lambda Expression Example: Foreach LoopTest it Now
ankit mayank irfan jai
Java Lambda Expression Example: Multiple StatementsTest it Now
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.Test it Now
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 Now
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 Now
Iphone 6S: 65000.0 Sony Xperia: 25000.0 Redmi4 : 26000.0
Java Lambda Expression Example: Event Listener