Lambda basics:-
whay lambdas?
-> helps functional programming
Advantages:-
Drawback of oops:
All code blocks should be associated with class and objects.
Lambda Expression examples
(int a) -> a * 2; // Calculate the double of a
a -> a * 2; // or simply without type
(a, b) -> a + b; // Sum of 2 parameters
If the lambda is more than one expression we can use { } and return
(x, y) -> {
int sum = x + y;
int avg = sum / 2;
return avg;
}
A lambda expression cannot stand alone in Java, it need to be associated to a functional interface.
interface MyMath {
int getDoubleOf(int a);
}
MyMath d = a -> a * 2; // associated to the interface
d.getDoubleOf(4); // is 8
whay lambdas?
-> helps functional programming
Advantages:-
- concise code
- parallel processing
Drawback of oops:
All code blocks should be associated with class and objects.
Lambda Expression examples
(int a) -> a * 2; // Calculate the double of a
a -> a * 2; // or simply without type
(a, b) -> a + b; // Sum of 2 parameters
If the lambda is more than one expression we can use { } and return
(x, y) -> {
int sum = x + y;
int avg = sum / 2;
return avg;
}
A lambda expression cannot stand alone in Java, it need to be associated to a functional interface.
interface MyMath {
int getDoubleOf(int a);
}
MyMath d = a -> a * 2; // associated to the interface
d.getDoubleOf(4); // is 8
No comments:
Post a Comment