[英]Why is lambda better in this code snippet?
I'm reading about lambda's on https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html 我在https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html上阅读有关lambda的内容
In Approach 1 they mention 他们在方法1中提到
"You would have to rewrite a lot of your API to accommodate this change. In addition, this approach is unnecessarily restrictive; what if you wanted to print members younger than a certain age, for example"
“您必须重写许多API来适应此更改。此外,这种方法不必要地受到限制;例如,如果您想打印小于特定年龄的成员该怎么办?”
public static void printPersonsOlderThan(List<Person> roster, int age) {
for (Person p : roster) {
if (p.getAge() >= age) {
p.printPerson();
}
}
}
In Approach 5 they use lambda expression: 在方法5中,他们使用lambda表达式:
printPersons(
roster,
(Person p) -> p.getGender() == Person.Sex.MALE
&& p.getAge() >= 18
&& p.getAge() <= 25
);
However, even in this lambda expression, we have to modify the API when we want to modify the search criteria. 但是,即使在此lambda表达式中,当我们要修改搜索条件时,也必须修改API。
Why is using a lambda here better compared to using a custom method in the first approach? 为什么在这里使用lambda比在第一种方法中使用自定义方法更好?
The problem with small examples is that they sometimes hide the real benefits. 小例子的问题在于它们有时隐藏了真正的好处。 In this case they are looking at a method that contains reporting logic.
在这种情况下,他们正在寻找一种包含报告逻辑的方法。 Approach 1 looks like this:
方法1如下所示:
public static void printPersonsOlderThan(List<Person> roster, int age) {
for (Person p : roster) {
if (p.getAge() >= age) {
p.printPerson();
}
}
}
The problem is, if you need other reporting, such as 问题是,如果您需要其他报告,例如
You will have to write a new method for each use case, where you re-implement the for-loop and printing instructions: 您将必须为每个用例编写一个新方法,在其中重新实现for循环和打印说明:
for (Person p : roster) {
if ( specificCondition) {
p.printPerson();
}
}
What you really want is to re-use all the logic around that condition and only re-write the specific search condition for your use case, without re-writing looping logic and print logic. 您真正想要的是重用围绕该条件的所有逻辑,并且只为您的用例重写特定的搜索条件,而无需重写循环逻辑和打印逻辑。
In approach 5, the printPersons
method takes a lambda as parameter. 在方法5中,
printPersons
方法将lambda作为参数。
printPersons(
roster,
(Person p) -> p.getGender() == Person.Sex.MALE
&& p.getAge() >= 18
&& p.getAge() <= 25
);
You can pass whatever search query to the printPersons
method, without having to write new methods like printMalePersonsOlderThanAndYoungerThan
where you have to re-write the full for-loop and all the printing instructions. 您可以将任何搜索查询传递给
printPersons
方法,而不必编写新的方法,例如printMalePersonsOlderThanAndYoungerThan
,在这里您必须重新编写完整的for循环和所有打印指令。
At the specific moment the printPersons
is written, the Person
getGender()
method might even not yet exist, so it would have been impossible to deliver a printMalePersonsOlderThanAndYoungerThan
a priori. 在写入
printPersons
的特定时刻, Person
getGender()
方法甚至可能不存在,因此不可能事先提供printMalePersonsOlderThanAndYoungerThan
。 So another advantage of approach 5 is that you can provide methods that are future proof and if your model changes, you will not have to write any more additional code than the specific condition. 因此,方法5的另一个优点是,您可以提供可以用于未来的方法,并且如果您的模型发生了变化,则无需编写除特定条件之外的任何其他代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.