title: How Set.contains() decides whether it's a subset or not?
body: <p>I expect the following code would give me a subset and a complementary set.</p>
  
  <p>But actually, the result shows that "Error: This is not a subset!"</p>
  
  <p>What it.next() get and how to revise my code to get the result I want?
  Thanks!</p>
  
  <pre><code>package Chapter8;
  
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.Set;
  
  public class Three {
      int n;
      Set&lt;Integer&gt; set = new HashSet&lt;Integer&gt;();
  
      public static void main(String args[]) {
          Three three = new Three(10);
          three.display(three.set);
          Set&lt;Integer&gt; test = new HashSet&lt;Integer&gt;();
          Iterator&lt;Integer&gt; it = three.set.iterator();
          while(it.hasNext()) {
              test.add(it.next());
              three.display(test);
              three.display(three.complementarySet(test));
          }
  
      }
  
      boolean contains(Set&lt;Integer&gt; s) {
          if (this.set.contains(s))
              return true;
          else 
              return false;
      }
  
      Set&lt;Integer&gt; complementarySet(Set&lt;Integer&gt; s) {
          if(this.set.contains(s)){
              Set&lt;Integer&gt; result = this.set;
              result.removeAll(s);
              return result;
          }
          else {
              System.out.println("Error: This is not a subset!");
              return null;
          }
      }
  
      Three() {
          this.n = 3;
          this.randomSet();
      }
  
      Three(int n) {
          this.n = n;
          this.randomSet();
      }
  
      void randomSet() {
          while(set.size() &lt; n) {
              set.add((int)(Math.random()*10));
          }
      }
  
      void display(Set&lt;Integer&gt; s) {
          System.out.println("The set is " + s.toString());
      }
  }
  </code></pre>