Lambda Expressions in Java
Lambda expressions in Java provide a clear and concise way to represent one method interface using an expression. They enable functional programming by allowing you to treat functionality as a method argument, or to use a method as data. Introduced in Java 8, lambda expressions simplify the code and make it more readable.
Lambda Expression in Java Syntax
The syntax of a lambda expression is as follows:
(parameters) -> expression
or
(parameters) -> { statements; }
Lambda Expression in Java Example
Here is a step-by-step example demonstrating the use of lambda expressions:
Without Lambda Expression in Java
import java.util.ArrayList;
import java.util.List;
public class LambdaExample {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("John");
names.add("Jane");
names.add("Max");
// Using anonymous class
names.forEach(new Consumer<String>() {
public void accept(String name) {
System.out.println(name);
}
});
}
}
With Lambda Expression in Java
import java.util.ArrayList;
import java.util.List;
public class LambdaExample {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("John");
names.add("Jane");
names.add("Max");
// Using lambda expression
names.forEach(name -> System.out.println(name));
}
}
Breakdown of the Lambda Expression in Java
In the lambda expression name -> System.out.println(name)
, we can see the following components:
- Parameter(s):
name
– This is the parameter passed to the lambda expression, similar to the parameter passed to a method. - Arrow token:
->
– This separates the parameters from the body of the lambda expression. - Body:
System.out.println(name)
– This is the body of the lambda expression. It specifies the actions to be performed when the lambda expression is executed.
Advantages of Lambda Expressions in Java
- Conciseness: Lambda expressions reduce the amount of boilerplate code required.
- Readability: Code is easier to read and understand.
- Functional Programming: Facilitates the use of functional programming techniques.
Functional Interfaces
A lambda expression can be used only in the context of a functional interface. A functional interface is an interface with a single abstract method. For example, the Consumer<T>
interface used in the above example is a functional interface from the java.util.function
package.
Example of a Custom Functional Interface
@FunctionalInterface
interface MyFunctionalInterface {
void myMethod();
}
public class CustomLambdaExample {
public static void main(String[] args) {
MyFunctionalInterface myFunc = () -> System.out.println("Custom lambda expression!");
myFunc.myMethod();
}
}
In this example, MyFunctionalInterface
is a custom functional interface with a single abstract method myMethod
. The lambda expression () -> System.out.println("Custom lambda expression!")
implements this method.
Conclusion
Lambda expressions provide a powerful way to write concise and readable code in Java, making it easier to implement functional programming concepts. They are especially useful when working with collections and streams, significantly reducing the verbosity of your code.