title: Java Iterate Over Collection
body: <p>I have a practice project which I need help with. It's a simple MailServer class. Here's the code:</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  import java.util.Iterator;
  import java.util.HashMap;
  import java.util.TreeMap;
  import java.util.Collection;
  import java.util.Map;
  
  public class MailServer
  {
      private HashMap&lt;String, ArrayList&lt;MailItem&gt;&gt; items;
  
      // mail item contains 4 strings:
      // MailItem(String from, String to, String subject, String message)
  
      public MailServer()
      {
          items = new HashMap&lt;String, ArrayList&lt;MailItem&gt;&gt;();
      }
  
      /**
       *
       */
      public void printMessagesSortedByRecipient()
      {
         TreeMap sortedItems = new TreeMap(items);
  
         Collection c = sortedItems.values();
  
         Iterator it = c.iterator();
  
         while(it.hasNext()) {
              // do something
         }
      }
  }
  </code></pre>
  
  <p>I have a HashMap which contains a String key (mail recipient's name) and the value contains an ArrayList of the mail for that particular recipient. </p>
  
  <p>I need to sort the HashMap, and display the each user's name, email subject, and message. I'm having trouble with this section.</p>
  
  <p>Thanks</p>