title: Using .max() with .stream() on a List<>
body: <p>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.
  The exercise is I have a <code>List&lt;&gt;</code>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.</p>
  
  <p>I was able to retrieve the highest Salary but not the Name of the employee, like this:</p>
  
  <pre><code>Integer maxSalary;
  maxSalary = roster
      .stream()
      .map(Employee :: getSalary)
      .collect(Collectors.reducing(Integer :: max)
      .get();
  </code></pre>
  
  <p>I was then given this small block of code and it works completely fine yet i am not quite sure why it works:</p>
  
  <pre><code>Integer maxSalary;
  Employee emp2 = roster
      .stream()
      .max((p1, p2) -&gt; 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.");
  </code></pre>
  
  <p>I understand it is a Lambda expression using <code>.max</code> i just cant seem to get my head around why and how it works?</p>