title: TreeSet and HashSet
body: <p>Why can't <code>HashSet</code> also be an answer for the following question? When I tested the code, both <code>HashSet</code> and <code>TreeSet</code> gave the same output, but the correct answer it seems is <code>TreeSet</code>. The code is below:</p>
  
  <pre><code>import java.util.*;
  
  public class Example {
      public static void main(String[] args) {
          // insert code here
          set.add(new Integer(2));
          set.add(new Integer(1));
          System.out.println(set);
      }
  }
  </code></pre>
  
  <blockquote>
    <p>Which code, inserted at line 4, guarantees that this program will
    output [1, 2]?</p>
    
    <ol>
    <li><code>Set set = new TreeSet();</code></li>
    <li><code>Set set = new HashSet();</code></li>
    <li><code>Set set = new SortedSet();</code></li>
    <li><code>List set = new SortedList();</code></li>
    <li><code>Set set = new LinkedHashSet();</code></li>
    </ol>
  </blockquote>