title: How to get nth element of a Set
body: <p>More specifically: how to get the nth element of a <a href="http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashSet.html" rel="noreferrer">LinkedHashSet</a> (which has a predictable iteration order)? I want to retrieve the nth element inserted into this <code>Set</code> (<em>which wasn't already present</em>).</p>
  
  <p>Is it better to use a <code>List</code>:</p>
  
  <pre><code>List&lt;T&gt; list = new ArrayList&lt;T&gt;(mySet);
  T value = list.get(x); // x &lt; mySet.size()
  </code></pre>
  
  <p>or the <code>toArray(T [] a)</code> method:</p>
  
  <pre><code>T [] array = mySet.toArray(new T[mySet.size()]);
  T value = array[y]; // y &lt; mySet.size()
  </code></pre>
  
  <p>Other than the (likely slight) performance differences, anything to watch out for? Any clear winner?</p>
  
  <h3>Edit 1</h3>
  
  <p><strong>NB:</strong> It doesn't matter <em>why</em> I want the last-inserted element, all that matters is that I want it. LinkedHashSet was specifically chosen because it <em>"defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set."</em></p>
  
  <h3>Edit 2</h3>
  
  <p>This question seems to have devolved into a discussion of whether <em>any</em> <code>Set</code> implementation can <em>ever</em> preserve original insertion order. So I put up some simple test code at <a href="http://pastebin.com/KZJ3ETx9" rel="noreferrer">http://pastebin.com/KZJ3ETx9</a> to show that yes, LinkedHashSet does indeed preserve insertion order (the same as its iteration order) as its Javadoc claims.</p>
  
  <h3>Edit 3</h3>
  
  <p>Modified the description of the problem so that everybody isn't too focused on retrieving the last element of the <code>Set</code> (I originally thought that the title of the question would be enough of a hint &mdash; obviously I was wrong).</p>