title: Average marks of students using Java streams
body: <p>Given a student class, I need to find all students whose name starts with 'A', in order of their average marks, using Java 8 stream API.</p>
  <p>I have tried the below code. The issue is I can easily find out students whose name starts with 'A' by filtering out, but how do I find out the average marks (as my marks are stored in a list - which has subject and score). Also how do I order them? These 2 issues I am completely clueless.</p>
  <p>Below is the code</p>
  <pre><code>public class Marks {
  
      private String subject; 
      private int score;
  
  // constructors ....
  // getter and setters..
  }
  </code></pre>
  <p>…and:</p>
  <pre><code>public class Student {
      
      private String name; 
      private int id; 
      private List&lt;Marks&gt; marks;
  
  // constructors..
  // getters and setters. 
  
  }
  </code></pre>
  <p>…and:</p>
  <pre><code>public class StudentMarks {
  
      public static void main(String[] args) {
          String name = null; 
          int id = 0; 
          
          int numberOfSubjects = 0;
          String subject = null; 
          int score = 0;
          
          List&lt;Student&gt; students = new ArrayList&lt;Student&gt;();
          List&lt;Marks&gt; marks = new ArrayList&lt;Marks&gt;();         
          
          for(int i = 0; i &lt; numberOfStudents; i++) {
              Student student = new Student();
              
              // code here to enter the name, id and number of subjects
              
              for(int j = 0; j &lt; numberOfSubjects; j++) {
                  Marks m = new Marks();
                  
                  // code to take the subject and score of the student..
                  
                  m.setSubject(subject);
                  m.setScore(score);      
                  
                  marks.add(m); 
              }
              
              student.setName(name);
              student.setId(id);  
              student.setMarks(marks);
              
              students.add(student);
          }
          
          // This would filter out the students whose name starts with 'A'
          List&lt;Student&gt; names = students.stream().filter(n -&gt; n.getName().startsWith(&quot;A&quot;)).collect(Collectors.toList());
          
          // Now second step (basically all this is to be done in one step) is to get the average marks and that too in some order (either ascending or descending. How do I do this ???
         OptionalDouble averageMarks = students.stream().filter(n -&gt; n.getName().startsWith(&quot;A&quot;)).mapToDouble(s -&gt; s.getMarks().get(0).getScore()).average(); 
         
          // I can't have get(0) above   
         //  How do I use mapToDouble to get the average as marks are stored in a list which has subject and score. I need to get the sum and then average for all students and then order them 
         // How do I use mapToDouble to sum the marks for each student and then get an average and then order them (ascending / descending).
      }
  }
  </code></pre>