preface

Java8 has some new features, today I will share with you about the origin of the Lambda expression, initially feel very bad readability, do not know why Java8 will come out this expression? Since there must be a reason for the leaders to launch it, it should be to solve some problems, so let’s see what problems have been solved.

case

The class director wants to know about each student:

1. Who is from Suzhou

The pseudo code above, is not very simple, write a method, directly filter out students in the Suzhou area.

The class director also wants to know which students are from Beijing, so the code can be optimized to dynamically transmit regional parameters.

2. Regional parameterization

Does that solve the query problem for all the locale as a parameter?

The problem seemed to be solved when suddenly the class director wanted to know the age distribution of the students.

Students over the age of 20 can be detected

This time it goes straight to the point, parameterizing age.

FilterStudentByArea and filterStudentByAge are two methods provided by the programmer because the class leader needs to find students by region and age.

Have you noticed that the codes in these two methods are very similar? There are many duplicate codes. But I think it’s okay to meet the needs.

modified

Because there are two methods above, many partners will think of changing the method to flag the business queried.

Change to a method where flag is true to find students by age range; Flag is false, indicating that students in the area are queried

Call way

List<Student> areaStudentList = filterStudent(students,"Suzhou", 0,false);
List<Student> ageStudentList = filterStudent(students,"", 20,true);
Copy the code

Although modified a method, but the implementation method is disgusting.

People say, well, don’t optimize, just do two methods. In fact, if the demand is unchanged, it is no problem, but if the class director and put forward to query according to the students’ results, or according to the weight of the students, height, etc.; Methods are written for each additional dimension, resulting in a lot of repetitive code.

As a motivated and ambitious programmer, this solution feels very twisted and needs to be solved. The JAVA design pattern comes to mind. Can we use this to solve the problem? Let’s see.

The strategy pattern

Fortunately, our friends know a little about design patterns, so let’s use strategic patterns for transformation

Define an interface for filtering. We can implement this interface to fulfill different requirements, such as:

There are two types of query conditions defined above that implement the StudentPredicate interface, so let’s look at the main method

The core code above is p.test(stu); This is the query condition logic implementation method; To query a business, you simply pass the interface implementation of the related business to the main method.

The above code is relatively elegant. If you have other requirements, just implement the StudentPredicate interface. However, this is also a disadvantage, each requirement has to write the associated interface implementation.

An anonymous class

To solve the problem of too many interface implementations, you can use anonymous classes to solve the problem, and then optimize it

This approach greatly reduces the number of interface implementation classes, and creates anonymous classes to implement business requirements when calling methods. The anonymous class approach is still not concise enough, so Java8 added Lambda expressions, let’s take a look.

Lambda expressions

Let’s look at the implementation of Lambda expressions

Let’s see if it’s neat, guys.

conclusion

The emergence of each technology is certainly to solve some problems, small partners in learning, must know why, what problem to solve, so that we will understand this technical point more in-depth, next article To say Lambda expression specific usage, thank you!!