title: How To Sort Object based on Object's attribute?
body: <p>I got everything else to work except the sort method. I need to sort the Students in the HashMap based on the Student's first attribute. I need the sort method to happen after I added all of the students in the HashMap, not while it's being added.     </p>
  
  <pre><code>package HashMap;
  
          import java.io.InputStream;
          import java.util.HashMap;
          import java.util.HashSet;
          import java.util.Scanner;
  
          public class clubMapping {
              HashMap&lt;String, HashSet&lt;Student&gt;&gt; map = new HashMap&lt;String, HashSet&lt;Student&gt;&gt;();
  
              public clubMapping(String clubName) {
                  InputStream is = getClass().getClassLoader().getResourceAsStream(
                          "student.txt");
                  Scanner scan = new Scanner(is);
                  while (scan.hasNext())
                      add(scan.next(), scan.next(), Integer.parseInt(scan.next()),
                              scan.next());
                  scan.close();
                  System.out.println(map);
                  System.out.println();
                  System.out.println(map.get(clubName));
              }
  
              public void add(String last, String first, Integer id, String club) {
                  HashSet&lt;Student&gt; set = new HashSet&lt;Student&gt;();
                  if (!map.containsKey(club)) {
                      set.add(new Student(last, first, id));
                      map.put(club, set);
                  } else {
                      set = map.get(club);
                      set.add(new Student(last, first, id));
                  }
              }
  
              public static void main(String[] args) {
                  new clubMapping("Math");
              }
          }
  
      package HashMap;
  
      public class Student {
          String last, first;
          Integer id;
  
          public Student(String l, String f, Integer i) {
              last = l;
              first = f;
              id = i;
          }
  
          public String toString() {
              return last + " " + first + " " + id;
          }
      }
  </code></pre>