title: Get all possible pairs of elements in a TreeSet
body: <p>Let's say I have a TreeSet of objects x. What I am trying to do, is iterate through all objects and get all possible pairs. So far, I have come to the code below</p>
  
  <pre><code>TreeSet&lt;x&gt; ts = new TreeSet&lt;&gt;();
  ts.add(new x(3, true));
  ts.add(new x(1, true));
  ts.add(new x(2, true));
  ts.add(new x(5, true));
  ts.add(new x(4, true));
  Iterator&lt;x&gt; iterator = setOfX.iterator();
      while (iterator.hasNext()) {
          System.out.println("ID:"+iterator.next());
          Iterator&lt;x&gt; innerIterator = setOfX.tailSet(iterator.next(), true).iterator();
          while (innerIterator.hasNext()) {
              int id = innerIterator.next().id;
              System.out.println(id);
          }
      }
  </code></pre>
  
  <p>But the output seems to luck some of the combinations, plus it exits with an exception</p>
  
  <pre><code>ID:1:true
  2
  3
  4
  5
  ID:3:true
  4
  5
  ID:5:true
  Exception in thread "main" java.util.NoSuchElementException
  </code></pre>
  
  <p>Can you point towards any solution?</p>