Xóa bài viết
Bạn có chắc chắn muốn xóa bài viết này không ?
Xóa bình luận
Bạn có chắc chắn muốn xóa bình luận này không ?
Java 8 – Lambda Expressions
https://grokonez.com/java/java-8-lambda-expressions
Java 8 – Lambda Expressions
Lambda expressions are new feature in Java 8. We now can represent one method interface by using an expression.
I. Overview
1. Pass a method as argument
Assume that we have 2 math operations: addition and subtraction. Those operations will be chosen in specific cases with 2 numbers: a and b. So the math operation is considered as an argument to method:
int doMathOperation(int a, int b, MathOperation mathOperation) {...}
To make thing done, in normal approach, we declare an interface:
public interface IMathOperation {
int run(int a, int b);
}
and two implementations:
public class Addition implements IMathOperation {
@Override
public int run(int a, int b) {
return (a + b);
}
}
public class Subtraction implements IMathOperation {
@Override
public int run(int a, int b) {
return (a - b);
}
}
Main function will be:
int doMathOperation(int a, int b, IMathOperation mathOperation) {
return mathOperation.run(a, b);
}
More at:
Bình luận

{{ comment.user.name }}
Bỏ hay
Hay

Cùng một tác giả

1
0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...

1
0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...

1
0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
Bài viết liên quan