Indeed, the Lambda expressions, together with the Stream API are the two most significant changes introduced in Java 8, as explained in this introductory document .
What are your benefits?
Here are some:
- we can create clearer and more concise code, since lambda expressions allow us to reference anonymous methods or unnamed methods, without having to resort to the use of anonymous classes. When a Lambda expression is written, it is translated into a functional interface at compile time. Here is an example of using Lambda expressions to replace an anonymous inner class with a much cleaner and readable code.
Without Lambda:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println(“Action Detected”);
}
});
With Lambda:
button.addActionListener(e -> {
System.out.println(“Action Detected”);
});
-
opens the door to functional programming in Java, where functions play a fundamental role. This means being able to pass functions, at runtime, as variable values, return values or parameters of other functions. This is a very powerful concept that can be understood as the possibility of passing behavior as a value and is precisely what we can achieve with the addition of lambda expressions to the Java language.
-
when used in combination with the Stream API, we can perform filter / mapping / reduction operations on data collections in a sequential or parallel manner and that its implementation is transparent to the developer.
-
When the expression lambda is composed of a single sentence and invokes some existing method by means of its name, there is the possibility of writing it using reference methods, which results in a more compact and easy to read code .
-
code writing is actually reduced. (See the example case presented in the article Introduction to Expressions Lambda
Do you have disadvantages?
Yes, it has one (although more than disadvantage we can call it a challenge, the challenge of improving ourselves, of continuing to learn).
To learn how to use Lambda expressions (as well as the Stream API), a paradigm shift is required in the way we've written Java code so far.
But overcoming this barrier has its reward, since we will have in our hands a very powerful tool that will make our lives easier.
Some examples to see the differences
1. Implementation of Runnable
//Sin Lambda:
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Sin Lambda");
}
}).start();
//Con Lambda:
new Thread( () -> System.out.println("Con Lambda") ).start();
2. Event management
// Sin Lambda:
JButton show = new JButton("Show");
show.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Sin Lambda");
}
});
// Con Lambda:
show.addActionListener((e) -> {
System.out.println("Con Lambda");
});
3. Iterating over lists
//Sin Lambda :
List features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API");
for (String feature : features) {
System.out.println(feature);
}
//Con Lambda:
List features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API");
features.forEach(n -> System.out.println(n));
4. Example of Map and Reduce
The code will apply 12% on each purchase
// Sin Lambda:
List costBeforeTax = Arrays.asList(100, 200, 300, 400, 500);
for (Integer cost : costBeforeTax) {
double price = cost + .12*cost;
System.out.println(price);
}
// Con Lambda:
List costBeforeTax = Arrays.asList(100, 200, 300, 400, 500);
costBeforeTax.stream().map((cost) -> cost + .12*cost).forEach(System.out::println);
Links