title: JAVA-8 streams collect advanced usage
body: <pre><code>package streams;
  
  import java.util.Arrays;
  import java.util.List;
  
  class Student{
      String name;
      int age;
      String type;
  
      public Student(){}
  
      public Student(String name, int age, String type) {
          this.name = name;
          this.age = age;
          this.type = type;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public int getAge() {
          return age;
      }
  
      public void setAge(int age) {
          this.age = age;
      }
  
      public String getType() {
          return type;
      }
  
      public void setType(String type) {
          this.type = type;
      }
  
      @Override
      public String toString() {
          return "Student{" +
                  "name='" + name + '\'' +
                  ", age=" + age +
                  ", type='" + type + '\'' +
                  '}';
      }
  
      public static List&lt;Student&gt; generateData() {
  
          List&lt;Student&gt; st = Arrays.asList(new Student("Ashish", 27, "College"),
                  new Student("Aman", 24, "School"),
                  new Student("Rahul", 18, "School"),
                  new Student("Ajay", 29, "College"),
                  new Student("Mathur", 25, "College"),
                  new Student("Modi", 28, "College"),
                  new Student("Prem", 15, "School"),
                  new Student("Akash", 17, "School"));
          return st;
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>//AdvancedStreamingP2 class uses the Student class
  
  package streams;
  
  import java.util.List;
  import java.util.Map;
  import java.util.Optional;
  import java.util.stream.Collectors;
  
  import static java.util.Comparator.comparing;
  import static java.util.stream.Collectors.*;
  
  public class AdvancedStreamingP2 {
  
      public static void main(String[] args) {
  
          List&lt;Student&gt; studentList = Student.generateData();
          System.out.println("\n---------- Extracting Student Name with Max Age by Type -----------");
          Map&lt;String, Optional&lt;Student&gt;&gt; stuMax = studentList.stream().collect(groupingBy(Student::getType, maxBy(comparing(Student::getAge))));
          stuMax.forEach((k, v) -&gt; System.out.println("Key : " + k + ", Value :" + v.get()));
      }
  }
  </code></pre>
  
  <p>I want to extract the student name, with the max age, grouping student by Type. Is it possible by using any combination in "collect" itself?</p>
  
  <p>I want the output like :</p>
  
  <pre><code>---------- Extracting Student Name with Max Age by Type -----------
  
  Key : School, Value : Aman
  Key : College, Value : Ajay
  </code></pre>