简体   繁体   English

在List <>上使用.max()和.stream()

[英]Using .max() with .stream() on a List<>

I would like to ask for an explanation to some code that I was given as a solution to an exercise I am doing in a Java course. 我想请求一些代码的解释,我将其作为我在Java课程中进行的练习的解决方案。 The exercise is I have a List<> of Employees with various properties(Salary, Name, Surname, E-mail...) and I am trying to retrieve the highest paid employee and print their Name and Salary. 练习是我有一个List<>的员工,有各种属性(薪水,姓名,姓氏,电子邮件......),我正在尝试检索收入最高的员工并打印他们的姓名和薪水。

I was able to retrieve the highest Salary but not the Name of the employee, like this: 我能够检索最高薪水而不是员工姓名,如下所示:

Integer maxSalary;
maxSalary = roster
    .stream()
    .map(Employee :: getSalary)
    .collect(Collectors.reducing(Integer :: max)
    .get();

I was then given this small block of code and it works completely fine yet i am not quite sure why it works: 然后我给了这个小块代码,它完全正常但我不太确定它为什么有效:

Integer maxSalary;
Employee emp2 = roster
    .stream()
    .max((p1, p2) -> Integer.compare(p1.getSalary(), p2.getSalary()))
    .get();
System.out.println("The employee who earns the most is :" + emp2.getName + " and earns : " + emp2.getSalary + " a month.");

I understand it is a Lambda expression using .max i just cant seem to get my head around why and how it works? 我知道这是一个使用.max的Lambda表达式我似乎无法理解为什么以及如何工作?

Optional<T> max(Comparator<? super T> comparator) explains it all. Optional<T> max(Comparator<? super T> comparator)解释了这一切。

Since intent of the question was to find the employee with the highest salary, stream is passed directly to .max which consumes an employee comparator . 由于问题的目的是找到薪水最高的员工,因此将流直接传递给.max ,这会消耗员工comparator Since comparator is functional interface, it can be passed in as lambda. 由于comparator是功能接口,因此可以作为lambda传递。 .max is already implemented version of more general reduce and collect operation available since java 8 .max已经实现了自java 8以来可用的更一般的reducecollect操作的版本

Integer.compare compares two int numerically. Integer.compare数字上比较两个int。 Therefore the .max() returns the employee with the highest salary. 因此.max()返回薪水最高的员工。

On the other hand your attempt is specifically trying to get the highest salary. 另一方面,你的尝试是专门试图获得最高薪水。

Cheers. 干杯。 Happy streaming. 快乐流媒体。

The Stream#max function: Stream#max功能:

Returns the maximum element of this stream according to the provided Comparator. 根据提供的Comparator返回此流的最大元素。 This is a special case of a reduction. 这是减少的特例。

As Comparator is a functional interface with only compare(T o1, T o2) to implement, it may be represented with a lambda. 由于Comparator是一个只需compare(T o1, T o2)来实现的功能接口,因此可以用lambda表示。 Here, the lambda (p1, p2) -> Integer.compare(p1.getSalary(), p2.getSalary()) compares the salary of p1 with the salary of p2 using standard integer comparison. 这里,lambda (p1, p2) -> Integer.compare(p1.getSalary(), p2.getSalary())使用标准整数比较将p1的工资与p2的工资进行比较。 Therefore, the result will be the Employee with the largest salary. 因此,结果将是薪水最高的Employee

max takes a function it uses to compare elements, max采用它用来比较元素的函数,

(p1, p2) -> Integer.compare(p1.getSalary(), p2.getSalary())

is a lambda - it's used to compare salary information from every element to find the greatest value. 是一个lambda - 它用于比较每个元素的工资信息,以找到最大的价值。

Optional<T> max(Comparator<? super T> comparator);

Returns the maximum element of this stream according to the provided Comparator. 根据提供的Comparator返回此流的最大元素。 This is a special case of a reduction. 这是减少的特例。

Comparator is a functional interface that allows to specify an order on a collection of objects of a specific type. Comparator是一个功能接口,允许在特定类型的对象集合上指定顺序。

Here is the functional interface method : 这是功能界面方法:

int compare(T o1, T o2);

So, when max() is invoked, you can pass to it a lambda that has as argument two Employee as the type of the Stream is Employee and provide a comparison implementation of two Employee objects as int compare(Employee o1, Employee o2) expects to : 因此,当调用max()时,您可以向其传递一个lambda,该lambda具有作为参数的两个Employee作为Stream的类型是Employee并提供两个Employee对象的比较实现作为int compare(Employee o1, Employee o2)期望至 :

.max((p1, p2) -> Integer.compare(p1.getSalary(), p2.getSalary()))

It's pretty simple: in your solution you convert Employees to their salaries and then find max number among numbers. 这很简单:在您的解决方案中,您将员工转换为工资,然后在数字中找到最大数量。 In the second (and correct) solution you don't perform any conversion: you compare Employees themselves by particular criteria . 在第二个(也是正确的)解决方案中,您不执行任何转换:您可以按特定条件比较员工自身。 And the criteria in your case is the salary. 你的案例中的标准是薪水。 So eventually max() gets not the max number, but the 'max employee' (by the salary). 所以最终max()不是最大数字,而是'max employee'(按薪水)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM